Gordon Brown has pledged continued UK support for Afghanistan in fighting the Taleban "for the next few years".
The prime minister visited British troops at Camp Bastion, the UK's largest military base in the country, as part of an unannounced visit.
His visit comes two days after a soldier with the 2nd Battalion The Yorkshire Regiment was killed in an assault on a Taleban stronghold.
Afghan and Nato forces were fighting to take Musa Qala during the PM's visit.
It is Mr Brown's first visit to Afghanistan as prime minister, but he visited the country when he was chancellor.
It also comes ahead of a statement he will make about Afghanistan to Parliament on Wednesday.
On this occasion, he travelled from Iraq where he has also been speaking to British troops during a surprise visit.
Speaking at Camp Bastion, Mr Brown told 150 of the UK's 7,000 troops in Afghanistan: "I want to thank all those who have been injured for their service and I want to remember all those who have given their lives in the service of their country."
Mr Brown told members of 40 Commando Royal Marines: "I want to thank every one of you for what you have done in what is the front line against the Taleban.
"This is one of the most challenging of environments, it's one of the most difficult of tasks, it's the most testing of times and it's the most important of missions because to win here and to defeat the Taleban and to make sure that we can give strength to the new democracy of Afghanistan is important for defeating terrorism all round the world."
Mr Brown continued: "When I speak of courage, I speak of men and women here who have shown huge bravery in really difficult circumstances.
"I know this weekend in Musa Qala some of you here have been doing a very important job in clearing the Taleban from that area."
In a speech made while international forces continued to battle the Taleban for Musa Qala, Mr Brown said: "I know that the work you are doing today and in the next few days is important for the whole future in Afghanistan.
"If we can succeed there it will mean we can move forward events in Afghanistan in favour of a more peaceful future for this country.
"People in Britain are incredibly proud of what you are doing."
Musa Qala leadership
Mr Brown then travelled to Kabul for talks with Afghan President Hamid Karzai.
Gordon Brown pledged support for Hamid Karzai's Afghanistan
He praised the leadership role of Afghan forces in fighting the Taleban, stressing that the Musa Qala battle was "led on the ground by the Afghan forces themselves".
"There is no doubt that succeeding in Musa Qala will make a huge difference both to how people see the weakness of the Taleban in the future and the ability of the government to build, not just militarily and politically, but with social and economic progress for the people of the area."
Musa Qala was taken over by the Taleban in February after a controversial peace deal brokered four months earlier between elders and the British, who withdrew after defending the area for months.
Speaking at a joint press conference with Mr Karzai, Mr Brown said military support would continue, as would support for social and economic development, including building schools, developing healthcare, and creating small businesses.
"I want to give President Karzai my assurance that our support will continue over these next few years to make it possible not only for the security of the Afghan people but also the economic and social development of your country so that people can have a stake in the future."
The Afghanistan visit comes after Defence Secretary Des Browne called for members of the international community to provide more troops to fight the Taleban.
He said the demands set by commanders from Nato's International Security Assistance Force were not being met and that the UK continued to ask countries for "additional support".