No-one was injured in the incident which took place at about 11:20 on Saturday but numerous wagons derailed and the track was damaged.
The line, which was closed for the weekend for engineering work, could remain shut for two weeks.
It is believed one of the trains ran into the back of the other.
A spokesman for British Transport Police said: "At about 11.20am on Saturday, 1 August, we were called following reports of a collision on the line near to Logan, East Ayrshire.
"Our officers attended and discovered two engineering trains had collided. As a result of the collision a seven wagons and one engine derailed, with some travelling down onto the embankment.
"Fortunately no-one was injured as a result of the collision or derailment, but the tracks have been significantly damaged and the line, which is the main Glasgow to Dumfries route, is expected to be affected for several days.
"At this stage the incident is not being treated as suspicious and the Rail Accident Investigation Branch has been informed."
A spokesman for track owner Network Rail told BBC Scotland that a crane was being brought in to remove the wagons.
He said this would take about 10 days and early indications were that it would then take about three days to repair the track.
The Network Rail spokesman said: "At the time of the incident the line was closed for engineering work and there was no public risk."
Train operator ScotRail Alliance said: "We apologise to customers affected by the disruption. Replacement buses have been arranged to minimise the impact and specialist engineers are working to reopen the line as quickly as possible."
Two trains carrying ballast for engineering work have collided on the track near Cumnock in Ayrshire, Network Rail has said.