British Prime Minister David Cameron said his country’s armed forces have paid a "very high price" for their involvement in Afghanistan.
With the UK drawing to the close of its 13-year military involvement in the country, Mr Cameron said Britain had gone "a long way" towards achieving its objectives there.
In a unannounced visit to Afghanistan, the prime minister met newly installed president Ashraf Ghani - who took office just four days ago - and chief executive Abdullah Abdullah.
The two presidential rivals finally came to a power-sharing agreement after a dispute over the outcome of the elections threatened to plunge the country into renewed turmoil.
Mr Cameron paid tribute to the 453 UK servicemen and women who have died in the course of operations in the country, as well as to those who had been injured.
"They (the armed forces) have paid a very high price for our engagement in Afghanistan. They have done vital work here," he said.
"We should remember those who paid the ultimate price and those who were injured through the work they did."
Mr Cameron, making his 13th visit to Afghanistan, said the country had been transformed since international military operations began in the wake of the 11 September 2001 attacks on New York and the Pentagon.
Mr Ghani told a joint press conference the Afghan people would remember that the UK stood "shoulder to shoulder" with them, and insisted that the military campaign had kept London safe.
Mr Cameron said driving out al-Qaeda had been in Britain's "national interest", and stressed that Afghanistan would not be "left alone" as UK troops pull out by the end of this year.
The British army has wound down its presence in the volatile southern province of Helmand after years of heavy fighting in some areas where the Taliban has launched fresh offensives in recent weeks.
Britain still has 3,900 troops in Afghanistan as part of the NATO's International Security Assistance Force (ISAF).
ISAF will complete its combat mission at the end of this year, with a follow-up mission taking over in 2015 on training and support duties assisting the Afghan army and police.
British contribution on the follow-up mission will be at an army officers' training academy outside Kabul.