An engineer noticed a significant bend in a rail and applied the emergency brakes before a passenger train derailed in western Kansas, an official has said.
The Amtrak train was apparently travelling at about 75mph when the engineer noticed the deformity in the rail and pulled the brakes, the official said.
The train carrying 131 passengers and 14 crew was travelling from Los Angeles to Chicago when it derailed shortly after midnight on Monday along a straight stretch of tracks in flat farmland near Cimarron, a small community about 160 miles west of Wichita.
Eight cars derailed and four of them ended up on their sides.
Thirty-two people were taken to hospitals for treatment and 29 of them had been released by late morning, Amtrak said. Ashley Rogers, a spokeswoman for Grey County, said none of the injuries were life-threatening. It was not known if any crew members were among the injured.
Daniel Aiken, 21, of Lenexa, Kansas, said he heard screaming as he climbed out of their overturned car. He stopped to smell a fluid that was flowing through the car, fearful that it was fuel, but was reassured when he realised it was water.
"Once people realised the train wasn't going to blow up," he said, "they calmed down."
"All the lights went out, it was dark," passenger Daniel Szczerba told Wichita TV station KWCH.
"People travelling in groups (of) four or five got thrown around the car as it turned over and lost people as they were trying to get out of the emergency exits."
Amtrak did not say how fast the train was travelling when it derailed.
It was foggy in the area at the time but it was not immediately known if that played any role.
Andy Williams, a spokesman for BNSF Railway, which owns the track, referred questions to Amtrak.
Amtrak said it is working with BNSF on the investigation.
Uninjured passengers were taken to the Cimarron community centre to wait for Amtrak to make arrangements to transport them to their destinations.
Kelsey Wilson, 21, said she woke up when she felt the ride "getting really bumpy" and the train started to shake. Ms Wilson, who was returning to Truman State University in Kirksville, Missouri, after spending spring break at home in Pueblo, Colorado, said her car disconnected from the one in front and that she hit her head as it overturned.
Ms Wilson said she escaped through the top of the flipped car then slid down the side before she "passed out". She was taken to a hospital and released with a neck brace.
Keith Holloway, a spokesman for the National Transportation Safety Board, said the agency was sending a team to investigate.