The Queen has been joined by many veterans at St Paul's Cathedral for a service commemorating the end of UK involvement in the Afghanistan war.
PM David Cameron gave a reading at the ceremony, which members of the Royal Family also attended.
A flypast and a parade by veterans also honoured British servicemen and women who fought and died in the 13-year conflict.
The Archbishop of Canterbury said it was "a moment for us to say thank you".
Simultaneous services were also held across the UK, in Afghanistan and Germany.
Earlier, Afghan president Ashraf Ghani paid tribute to the 453 UK troops who died in the conflict, saying they had "paid the ultimate sacrifice to enable us to live in freedom, in hope for peace, prosperity and dignity".
The Duke of Edinburgh, the Prince of Wales and Duchess of Cornwall, the Duke and Duchess of Cambridge and Prince Harry - who served two tours during the conflict - were also at the ceremony.
During the service the Archbishop of Canterbury, the Most Reverend Justin Welby, blessed a cross made of shell casings which had been on a memorial wall in Camp Bastion, the main allied base in Afghanistan.
The cross is being moved to the Royal British Legion's National Memorial Arboretum.
The Dean of St Paul's, the Very Reverend Dr David Ison, gave thanks for the commitment of service personnel, aid agencies and support organisations in that country.
He also commemorated "with sorrow and regret" those who were killed and asked for prayer for the Afghan nation, calling for remembrance of the "danger which people in that great country continue to face today".
The service was followed by a military parade through the City of London to a reception at Guildhall, and a flypast of aircraft used in the campaign.
The aircraft included Chinook, Apache and Sea King helicopters and Hercules and Tornado aircraft.
Dave Hart, who served and was injured in Afghanistan, said: "I think today really is primarily, for me, about the comrades who didn't come home.
"And those who have come back with injuries - be they physical or mental - and also for their families. It does feel like a closing of a chapter really."
Similar ceremonies have also been held at military bases and churches across the UK, as well as in Germany.
The event in Afghanistan at the Afghan National Officer's Academy was attended by 150 personnel and some Afghan dignitaries.
At the scene
Caroline Wyatt, BBC religious affairs correspondent
In the cold spring sunlight, it was the mothers who stood out as they waited patiently in line to enter St Paul's - as all had waited patiently for news of their sons serving in Helmand - before receiving the call that all military families dread.
Many of the bereaved were dressed in black, but several mothers wore a blood-red knitted poppy, made by some of the mothers themselves to raise money for military charities for care of the wounded.
Few of the military families here had easy answers when asked about whether the war had been worth it.
For the bereaved, all said the pain of the loss of their son, husband or father would never go away, although for some, the grief eased with time, though the tears were never far away.
Waiting in line with them were the wounded, many bearing the obvious scars of combat, some in wheelchairs, others walking on the prosthetic limbs they have become accustomed to over many painful years.
They, too, hesitated when asked about the human cost of the campaign, saying only history could judge.
UK forces were part of a US-led coalition which invaded Afghanistan and toppled the ruling Taliban in 2001, following the 9/11 attacks in the US.
At the peak of the 13-year campaign the UK military had 9,500 troops and 137 bases in Helmand Province.
The UK ended its operations in October, while Nato finished its mission in December.
Earlier, Sarah Adams - mother of Pte James Prosser who was killed in Afghanistan in 2009 - told the Today programme: "James believed in what he was doing, and I have to try to accept that, that he wanted to do that and believed he should be there. But as a mum, it's very difficult.
"We did achieve a lot but we didn't make it the safest place and it's still ongoing. Afghan people are still losing their lives because of the Taliban."
Former soldier Craig Gadd, now of Help for Heroes, lost his left leg after stepping on an improvised explosive device in Afghanistan.
He said: "Things happen. I feel very lucky that I only lost one leg. Some of my colleagues have injuries far worse than mine. I've done some amazing things since that day."
Lance Corporal James Ashworth was killed in Afghanistan in 2012 and posthumously awarded the Victoria Cross. His father Duane Ashworth said he believed the country was in a better position than it was before UK involvement.
"It's now for the forces that are there to continue that training with the Afghan forces and police, to make sure what our lads did does continue."
In an interview with Forces TV, former PM Tony Blair said: "I always felt that it was right and justified that we were there in Afghanistan, that we were fighting both to remove the Taliban and then to try and stabilise the country.
"But there is nothing that's really possible to say that could provide true consolation for the family of someone who has lost their life."
Defence Secretary Michael Fallon said: "Afghanistan has taught us these operations take time, you need to be patient, you need to help build resilience, you need to concentrate on developing and improving the economy of the country - as well as the military effort to defeat terrorism itself."
Some British combat troops still remain in Afghanistan in training, advisory and support roles.
As well as paying tribute to Britain's war dead, President Ghani spoke of the enduring legacy of the UK's commitment at the British-run officer training academy near Kabul.
BBC Afghanistan correspondent David Loyn said President Ghani's words contrast strongly with complaints by his predecessor President Karzai, who said that the British presence had made things worse in Helmand.