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.
Mrs Nightingale added that "justice had 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.
The judge who sentenced Sgt Nightingale on November 7 said the sniper may still have a future in the army.
Sgt Nightingale said that he could not answer questions about his future.