A freight train carrying diesel has derailed and burst into flames.
About 100 people living within 800m of the blaze in Llangennech, near Llanelli in Carmarthenshire, were evacuated from their homes on Wednesday night, but have since been allowed to return.
Natural Resources Wales is assessing the impact of the diesel spill.
The two workers who were onboard the train have been accounted for and no injuries have been reported, British Transport Police (BTP) said.
The Rail Accident Investigation Branch (RAIB) said it was carrying out a preliminary examination and gathering evidence.
The fire was reported at about 23:20 BST on Wednesday and police said it had been declared a “major incident” due to the “initial risk of harm” to people nearby.
Louise Perkins, who lives close to the railway line, said she grabbed some stuff and rushed from her home with her children, aged seven, 12 and 19, in the middle of the night.
The 38-year-old was up late watching TV when she heard an enormous bang: “There was this massive fire and black smoke.
“As we left it was just fire – you could see like balls of it puffing up, and the smell… the smell of diesel.
“It was just everywhere, and there were just black, black clouds of smoke in the air.”
Ms Perkins said she was “quite worried” when police explained “it could explode”.
“At that point my children were up and and they got really distressed,” she said.
Her 19-year-old son went to his friend’s house while her two younger children went to their grandmother’s.
“They were worried – ‘oh, what if our house catches fire, what about all the stuff?'” she explained.
“I told them not to panic. But my youngest was really upset, she was just heartbroken, she was crying.”
The site of the derailment is close to the Loughor Estuary, which is part of the Carmarthen Bay and Estuaries Special Area of Conservation.
NRW said: “Our officers have attended the diesel train fire and are providing advice to help manage the environmental impact of the diesel spill.
“We will fully assess the impact once the fire is under control and it’s safe to do so, and will provide further updates during the day.”
One Twitter user said they could smell burning fuel from “well over a mile away”.
Sandra Thomas has lived on Pontarddulais Road since 1972 and said nothing like this had ever happened before and the fire went up “like a bomb”.
“I was terrified – I’m shaking all over,” she said, adding police knocked on her door while she was watching TV in bed.
She was sent to the community centre, but was allowed to return home on Thursday morning.
But Ms Thomas became concerned when she looked out of her window at 08:00 to see a large black plume of smoke.
“I don’t feel 100% safe in case it sparks off again and the rest of the tankers go up,” she added.
BTP said it was “securing key witness statements and CCTV” and would be working with the Office of Rail and Road and the RAIB “to establish the exact circumstances behind the incident”.
Assistant Chief Constable Sean O’Callaghan said: “The fire continues and there is still a substantial risk within its immediate vicinity, so our advice remains for people to avoid the area.
Supt Ross Evans of Dyfed-Powys Police said: “The evacuation no doubt caused a significant amount of disruption for those evacuated in the middle of the night and I would like to personally thank residents for their response, as well as our frontline officers who swiftly moved hundreds of residents out of their homes.”
Those evacuated had been asked to meet at Bryn School and Llangennech Community Centre, police added.
At the scene – BBC Wales reporter Kathy Peart
Even from a distance, the strong smell of diesel hangs in the air and this morning the fire seemed to kick off again and thick black smoke began rising from the crash site.
I can see the hoses in action where the train is on the tracks, but the view is blocked somewhat by the trees.
The fire has since been damped down again and the smoke has turned white again.
Gary Jones, a councillor for Llangennech, said the fire “seemed to die down and then there seemed to be fresh explosions”.
“The height of it was incredible,” he told BBC Radio Wales.
Mr Jones said about 100 people were evacuated and up to 40 went to the community hall, adding: “Everyone has gone home now. The village is blocked, you can’t leave the village at the moment.”
The A4138 is closed from Troserch Road to junction 48 of the M4 at Hendy, and the B4297 between Station Road and the A4138 is also closed.
The fire has also resulted in delays on passenger trains between Llanelli and Llandrindod Wells in Powys, Transport for Wales said.
All lines between the two stations have been closed and TfW said it expected disruption on all routes throughout Thursday.
Buses will be put on for services affected by the fire.
Ryan Milson, a photographer and reporter for the Pembrokeshire Herald newspaper, said the train was taking thousands of litres of diesel in 22 tanks from Milford Haven in Pembrokeshire to Reading when it derailed.
He reported eight fire appliances working on the fire at one point.
Mr Milson said a locomotive was being used to transport a number of carriages away from the fire.