The decision clears Mr Besigye to begin campaigning against his rival, President Yoweri Museveni, a former friend, in elections next month after registering from his prison cell to compete. He is the first credible challenger to the President's 19-year rule.
Mr Besigye, 49, was greeted by more than 12,000 supporters when he left the high court in Kampala after spending Christmas in a cell. Police fired live bullets and tear gas as the crowd surged forward to greet him as he drove away from the courthouse in an open-topped vehicle.
Mr Besigye has been in prison since he returned to Uganda in November after four years in exile, facing charges of treason and rape. He faces charges of terrorism and the illegal possession of firearms in a separate military tribunal.
Judge John Bosco Katutsi decided at the high court yesterday that the military court had no right to detain him because the civilian court had suspended the military trial. Mr Besigye's lawyers successfully argued that he could not receive a fair trial before the military court as it was controlled by Mr Museveni's aides. He will return to the high court tomorrow for a hearing on the rape charges.
Mr Besigye denies all charges against him, and his party, the Forum for Democratic Change, says the cases are designed to keep him from running in next month's presidential elections.
After his release he told reporters: "We shall continue to struggle against oppression. This government is falling.
"There are so many people in illegal detention like me, and we shall struggle by all possible means to restore good governance in this country."
Britain and other European Union countries have suspended aid to Uganda over alleged human rights abuses. Just before Christmas, Britain said it would withdraw £15m in aid that it gives directly to the Ugandan government, and channel it instead through the UN to help civilians displaced by conflict. It has also withheld a further £5m until it is satisfied that the forthcoming elections are free and fair. Norway, Ireland, the Netherlands and Sweden have also suspended direct aid.
The imprisonment of Mr Besigye was raised at the Commonwealth summit last month in Malta.
Mr Museveni has said in public that he relishes the chance to beat Mr Besigye at the polls, but he is thought to have been shocked at the rapturous welcome his rival received when he returned to Uganda from South Africa in November.
Mr Besigye was once President Museveni's personal doctor, and supported him throughout the guerrilla war that brought Mr Museveni to power in 1986. The two fell out in 1998, after Mr Besigye published an article accusing the President of becoming the type of African autocrat he swore he would never be. He eventually fled Uganda in 2001, saying that Mr Museveni was trying to kill him after he stood unsuccessfully in an election against him.
President Museveni had been fêted by the international community in the 1990s as a new breed of African leader, and Uganda attracted millions of pounds of direct aid. But the gloss began to come off the country's image when Uganda joined its neighbours in invading the Democratic Republic of Congo in 1998, but it was Mr Museveni's recent decision to change the constitution to allow him to stand for a third term in office that struck the final blow for his image. His critics say he has become another of Africa's "big men" who seek to hold power for life.