Work at the yard was suspended last night and an inquiry launched.
Emergency services were called near Wharf St at 5pm, with up to a dozen staff working to comfort and then free the trapped man.
After half an hour, the audibly upset man, who was helped by St John ambulance staff, was finally freed by firefighters and taken to Dunedin Hospital.
A KiwiRail spokeswoman said the wagon appeared to have collided with a locomotive pulling a rake of wagons at the yard.
Asked if was normal practice for a person to work at the front of a moving wagon, she replied yes.
It was too early to say what caused the accident, she said, and a full internal inquiry had been launched and the Department of Labour was investigating.
Acting Senior Sergeant Ed Baker said the man worked as a shunter for KiwiRail and was positioned near the front of the train at the time of the incident.
Initial inquiries indicated the trains were sharing the same track, he said.
The Otago Daily Times understands the man was involved in the moving of freight from the Fonterra coolstore at Mosgiel.
Shunters work to couple locomotives with wagons, often using the running board to gain access to the locomotive.











