The youngster was spotted by Martin McManus, the driver of a ScotRail train travelling in the opposite direction between Wester Hailes and Kingsknowe, which are a mile apart.
British Transport Police (BTP) said the boy had got off the train at Wester Hailes on Thursday afternoon without his family.
He was later was found on the tracks and picked up by the other driver.
Off-duty Police Scotland officers on that train took him to Kirknewton, where the boy was handed to other officers who reunited him with his mother at Wester Hailes.
ScotRail said the train driver, Martin McManus, had said: ‘He was very lucky. I was very shocked seeing this small child.
‘I saw the wee lad running along the track crying, so I stopped my train and made sure it was safe.
‘I went out and picked him up and brought him back in. He was looking for his mummy and was just asking if his mummy was on the train.
“He was halfway between Kingsknowe and Wester Hailes station when I saw him.
“I haven’t a clue how he got off the train or ended up down there.
“It could have been really tragic so I was lucky I spotted him but it’s all just part of my job.”
Passenger Bonnie Nicolson praised Mr McManus by posting a picture of him on Facebook.
She wrote: “Here is the amazing train driver who saved a little boy’s life today when he noticed the little one on the tracks on the outskirts of Edinburgh & managed to stop the train on time.
“I was so traumatised I forgot to ask his name but what a hero.”
A BTP spokesman said: “A four-year-old boy was found by a driver along the tracks.
“He was later taken to Wester Hailes and reunited with his family.”
A spokeswoman for the ScotRail Alliance, which includes track owner Network Rail, said: “Our staff are trained to look out for vulnerable passengers, and on this occasion our driver took swift action to ensure the child’s safety.”