Uganda's Constitutional Court has quashed the treason charges against opposition leader Kizza Besigye.
The Forum for Democratic Change leader had challenged the legality of the charges he and 10 others had faced in both civilian and military courts.
In a unanimous ruling, the panel of five judges said there was no way of guaranteeing a fair trial.
Dr Besigye is set to take on President Yoweri Museveni for a third time in elections next year.
After the judgement, Dr Besigye told the BBC's Focus on Africa programme he welcomed the ruling as the court "had condemned the actions of the state".
He said it was a relief to be able to get his passport back and that his reputation had been restored.
"All the trumped-up charges that have been brought against me have been put to a final stop," he said.
Dr Besigye used to be Mr Museveni's personal doctor and the two men were allies in the guerrilla war which brought Mr Museveni to power in 1986, before they fell out.
After losing the 2002 poll, Dr Besigye fled Uganda, saying he feared for his life.
He returned before the 2006 election, but was not able to campaign properly as he was charged with supporting an armed group and rape.
He was cleared of the rape in March 2006 - and has said all the allegations were part of a campaign of political persecution.