The transport secretary, Chris Grayling, said there would be an investigation into how the incident occurred on Wednesday morning near Port Talbot.
A third person was treated for shock at the scene but uninjured. British Transport Police (BTP) said it was too early to confirm whether the section of railway between Pyle and Port Talbot was being treated as a crime scene, but confirmed forensics officers were in place.
Network Rails Wales route director, Bill Kelly, said it was shocked and distressed by the dreadful accident and fully cooperating with investigators.
Our thoughts are with the families of our colleagues and our members of staff who will be affected by this tragic loss, and we will provide all the support we can, he said. Union bosses called for a full investigation into the deaths.
Manuel Cortes, the TSSA general secretary, said: Its too early to speculate about what has happened here, but clearly something has gone badly wrong. There must now be a full investigation because it is simply not acceptable that in the 21st century, people go out to work and end up losing their lives.
Our Network Rail members, together with everyone else at the company, do so much to keep our railways running smoothly. They must be able to do this in a safe environment.
Safety on our railways is paramount and sadly, as todays tragic events show, it can never be taken for granted.
Mick Cash, the general secretary of the RMT union, said: This is shocking news. RMT is attempting to establish the full facts, but our immediate reaction is that this is an appalling tragedy and that no one working on the railway should be placed in the situation that has resulted in the deaths that have been reported this morning.
As well as demanding answers from Network Rail and a suspension of all similar works until the facts are established, the union will be supporting our members and their families at this time. Our thoughts are with those involved in this incident and their loved ones.
Network Rail was unable to confirm whether or not works were being carried out on the track at the time of the deaths.
The incident resulted in cancelled trains, with replacement buses being put on for rail passengers.
Investigators from the Rail Accident Investigation Branch and the Office of Rail and Road attended the scene.
The BTP Supt Andy Morgan said: We are making a number of urgent inquiries to understand exactly what happened in the moments leading to this fatal collision.
Very sadly, two people have died, and my officers are now working to identify them and ensure that their families are supported at this incredibly difficult time.
A man who said he worked at the nearby Tata steelworks said: I didnt see a great deal, by the time I got close paramedics were giving one of the workers CPR, but he sadly passed away. The other worker had already passed away.
Stephen Kinnock, the local Labour MP, said: Im very concerned to see reports of the tragic accident on the rail line between Bridgend and Port Talbot. This is awful news and my thoughts are with the families of all concerned.