The Royal Highland Fusiliers, 2nd Battalion The Royal Regiment of Scotland, paraded through Penicuik, Midlothian, following their third tour in the country.
Onlookers cheered and waved union flags as around 400 soldiers marched to the town centre where they were saluted by the Duke of York and local dignitaries.
The battalion scored an early victory in the "battle of the weather" as the rain held off for the parade but they were greeted by a torrential downpour on their return to Glencorse Barracks.
Campaign medals were presented to distinguished soldiers, and Prince Andrew paid tribute to the battalion and their families.
"Apart from the fact that today has been dreich, you have been welcomed back to a very happy Scotland," he said. "I would like to say thank you on behalf of Her Majesty for what you have done, and I would like to extend my very best wishes to you, the battalion, as you go about your future duties.
"Congratulations to those who have received your campaign medals. Thank you to everyone for your dedication and loyalty."
Three soldiers from the battalion were killed in Afghanistan and six were hurt in an incident on April 30. They were on patrol between bases in Lashkar Gah when their vehicle was struck by an improvised explosive device.
Lt Col Robin Lindsay, commanding officer, said: "We are delighted to be back but that delight is tinged with sadness because we lost three of our brothers-in-arms in Afghanistan, who were all dear to us. It was a very painful incident, but it brought the battalion together and gave us real determination to complete our mission in Afghanistan. Our thoughts are very much with their families today.
"The battalion look back on the tour with satisfaction because Afghanistan over the course of 10 years has become unrecognisable, and certainly central Helmand. I was there in 2009 and some areas like Nad Ali were very insecure with fractured communities, but nowadays there is a flourishing bazaar and local elections where 10 times as many people voted than three years ago.
"The people of Penicuik are incredibly supportive of their local battalion, and so we're really grateful for their support."
Major Tim Draper, 36, from Edinburgh, said: "It was a busier place for us when I first went out in 2009. We were the people in and amongst the fighting back then and while there is much less fighting almost everywhere, now that fighting is on the whole under the control of the Afghans.
"For a lot of the remaining fighters, they are reacting to local issues: some tension in the local area that they are concerned about.
"I'm not sure if the average insurgent on the ground is out to fight for some kind of higher cause. It's much more personal, and much more local."