Uganda's High Court has released on bail opposition leader Kizza Besigye, who is running in elections next month.
Kizza Besigye is ahead in a poll, despite being unable to campaign
He was imprisoned in November on treason, rape, firearms and terrorism charges after returning from exile.
Outside the courthouse in Kampala, police fired tear gas to disperse celebrating opposition supporters.
A judge said the extension of a military warrant keeping Dr Besigye in prison over additional terrorism charges was illegal.
"The applicant has been under illegal detention. He should enjoy his bail as granted by the High Court," said Justice John Bosco Katutsi.
"The accused should be released forthwith," he ordered.
Dr Besigye returned briefly to the High Court after his release for the start of his rape trial - before it was adjourned until Wednesday.
At that trial, a 28-year-old woman told court that Doctor Besigye raped her twice in back in 1997.
Police moved to break up celebrating crowds
After his release, thousands took to the streets and joined Dr Besigye in a lively, yet peaceful, procession towards his party headquarters.
Military police wielding batons beat some of the supporters in an attempt to move them on.
This induced some panic and some questioned why force was being used against them.
Dr Besigye's supporters say the multiple charges in civilian and military courts are intended to stop him campaigning ahead of February's elections, in which he is facing President Yoweri Museveni.
The BBC's Will Ross says the court ruling was a victory for Dr Besigye's lawyers, but there are many battles ahead.
Kizza Besigye was once President Museveni's personal physician
Attorney General Khiddu Makubuya said he was waiting for a full copy of the judge's ruling before reacting.
Our reporter says it is possible that despite the judge's ruling, the military will move to re-arrest him because the military court has stated that it is not subordinate to the High Court.
Following the judge's decision, Dr Besigye said the government was still determined to violate his rights and warned that this was not the end.
"I can't say I'm excited. I'm relieved but not excited because we are dealing with a regime which is determined to act illegally, to violate our rights and this is not going to end today. This is just one demonstration of their illegality but it's not the end."
The Forum for Democratic Change presidential candidate also faces treason charges in a case to begin at the High Court on Friday.
The retired army colonel denies all the charges, saying they are politically motivated.
He returned from five years abroad to contest next year's polls and is viewed as the first credible challenger to Mr Museveni, who has been in power for 19 years.
These will be the first multi-party elections since Mr Museveni took power.
A small urban-based opinion poll conducted by a Ugandan newspaper suggests Dr Besigye is ahead of Mr Museveni by several percentage points - although Mr Museveni is likely to have strong support in rural areas.
Several Western donors have withdrawn direct financial support for the government in recent months, to register their concerns over democratic reforms.
Dr Besigye is expected to attend his first rally since the start of the official election campaign in Kayunga District, east of Kampala, on Tuesday.