The former Sri Lankan army chief, Sarath Fonseka, has been freed after more than two years in prison.
Sri Lanka's President Mahinda Rajapaksa signed the release order over the weekend, but legal formalities had to be completed before he could walk free.
He was arrested two years ago after standing against Mr Rajapaksa for the presidency when the two men fell out.
"I will devote my life [to] my people," he shouted as he emerged from Welikada jail, the AFP news agency reported.
As firecrackers set off by his delighted supporters reverberated across Colombo, Mr Fonseka promised not to abandon his opposition to the government.
"I will sacrifice my life to serve the people of this country," he said, "even if they put me in prison for 10 years, I will not stop performing my duty."
His release came after Foreign Minister GL Peiris met US Secretary of State Hillary Clinton on Friday in Washington.
The human rights situation in Sri Lanka was said by officials to be high on the agenda. The US describes the former army officer - who has been stripped of his rank as a four-star general - as a political prisoner.
As military leader he was widely credited with the 2009 victory over Tamil Tiger rebels. But Mr Fonseka was jailed for corruption in 2010.
He emerged from the high security prison to be greeted by 2,000 supporters who chanted: "Victory to our war hero! Victory to our leader!"
Fonseka kissed his hands and raised them to the crowd, some waving the national flag. He then released a white dove symbolising peace.
Earlier, he was also mobbed by supporters as he left a hospital where he had been receiving treatment for a respiratory condition.
He then attended to legal formalities at the Supreme Court before returning to jail prior to being freed.
"My medical condition has improved. I salute the people who wished me well. I salute the staff of the Nawaloka hospital," he said after his release.
Supporters say he was freed because of international pressure from human rights groups.
"He was a political prisoner and should have been set free long ago," one of them, Athula Dhabare, said outside Welikada Prison.
"He is the true leader who liberated this country from 30 years of war."
Correspondents say the President Rajapaksa has until now defied UN and US criticism of his record - and Mr Fonseka's release is partly aimed at distracting attention from his government's alleged poor economic management.
Last week, news of Mr Fonseka's impending freedom was welcomed by investors who suggested that it would improve the country's international image and prompt more foreign financial investment.
Sri Lanka marked the three-year anniversary of the end of the 26-year civil war at the weekend, and held a large military parade in the capital, Colombo.
The war began in the 1980s, with Tamils pressing for self-rule against a backdrop of an increasingly assertive Sinhalese nationalism.
The violence killed up to 100,000 people over several decades, with accusations that both sides in the conflict committed war crimes against civilians.
Rights groups say that Mr Fonseka and President Rajapaksa are implicated in shooting Tamil fighters as they sought to surrender. Both men deny the allegations.