An SAS sniper thanked the "great British public" after he was released from jail.
Sgt Danny Nightingale, 37, was sentenced to 18 months' military detention for illegally possessing a Glock 9mm pistol and more than 300 rounds of ammunition.
But three appeal judges found that the sentence was too harsh, cut the term to 12 months and said it should be suspended, and ordered Sgt Nightingale's release.
The SAS sniper's wife Sally burst into tears as the ruling was delivered.
Outside the Court of Appeal in London, Sgt Nightingale said the level of public support had been "humbling" before he kissed his wife.
- Warning: This video contains flash photography.
Judges heard that more than 100,000 people signed a petition calling for Sgt Nightingale to be released after Sally, 38, and other family members launched a campaign.
– Sgt Danny Nightingale
Thank you to the great British public. They have been absolutely wonderful in their support. It has just been extremely humbling. Very, very humbling.
Mrs Nightingale added that "justice had been done".
– Sally Nightingale
It can only be good for all the troops out there fighting for our country to see justice has been done.
Judges heard that the gun had been a gift from Iraqi special forces soldiers Sgt Nightingale had trained.
The SAS sniper had planned to have the weapon decommissioned and keep it as a trophy, the court heard.
ITV News correspondent Geraint Vincent reports:
The judges were also told thatSgt Nightingale, who has suffered medical problems affecting his memory, had appeared to put the gun in a cupboard in his army accommodation in Hereford on a "very hectic day" when preparing to deploy to Afghanistan.
Sgt Nightingale, whose accommodation was not on the SAS base at Hereford, said he had not "appreciated" that he had the weapon.
Defence Secretary Philip Hammond told ITV News it was not for him to judge whether Sgt Nightingale should have been sent to prison.
He added that the court "acted swiftly" in this case which showed the system was "robust":
Julian Brazier, MP for Canterbury and Whitstable and a former SAS reservist officer, who has campaigned on behalf of Sgt Nightingale, welcomed his release.
– Julian Brazier MP
I was delighted to hear the news.
While firearms offences are normally very serious, given the very unusual circumstances of the case, and Sgt Nightingale's remarkable record of service, the original sentence was a serious miscarriage of justice.
I am delighted that Danny will be going home to his family for Christmas.
The judge who sentenced Sgt Nightingale on November 7 said the sniper may still have a future in the army.
– Judge Alastair McGrigor, who sat with a panel of Army officers
We have not dismissed you or reduced you in rank.
We consider that you may still, with your specialist experience, be of use to the Army in the future.
Sgt Nightingale said that he could not answer questions about his future.