Prince William, his pregnant wife Kate, and Prince Harry -- who served two tours during the conflict -- also attended the ceremony at London's St Paul's Cathedral to remember the war dead.
Britain was part of a US-led coalition that fought the Taliban in Afghanistan following the September 11, 2001 attacks. Almost 150,000 Britons served in the conflict, and 453 died.
"Today is a moment for us to say thank you: Thank you to all who served, whatever your role," Justin Welby, the Archbishop of Canterbury said during the service.
A few hundred soldiers remain in advisory roles to help the Afghan army.
Afghan President Ashraf Ghani told the BBC that the Britons who gave their lives in his country had "paid the ultimate sacrifice to enable us to live in freedom, in hope for peace, prosperity and dignity."
The international coalition's mission ended in December, leaving Afghan security forces to fight the Taliban alone.
Last year was the deadliest since the insurgency began, and many expect the Afghan government to be tested this year once the fighting season begins in spring.
Former Prime Minister Tony Blair said earlier in an interview that sending troops to Afghanistan was "right and justified," although he had not foreseen how long the struggle would last when the conflict began.
"I think we have not yet understood the depth of this problem, the scale of it, and the need for a comprehensive strategy to deal with it," he told Forces TV.
