Kuala Lumpur, Malaysia (CNN) -- A Malaysian court on Monday found opposition leader Anwar Ibrahim not guilty of sodomizing a former male political aide, bringing a conclusion to a politically charged trial that has lasted nearly two years.
The courtroom erupted in cheers from Anwar's supporters after the verdict and some of his family members burst into tears.
"Justice has been done," Anwar said as he left the court building, walking into a crowd of camera-wielding journalists and supporters. "I feel I have been vindicated."
About 2,000 of his supporters had gathered outside the court building Monday ahead of the announcement of the verdict. They had begun arriving in a nearby parking lot before sunrise, accompanied by a heavy police presence.
Anwar had been on trial in the Kuala Lumpur High Court on allegations that he sodomized the former aide, Saiful Bukhari Azlan, in June 2008. Sodomy, even if consensual, is an offense punishable by up to 20 years of prison in Malaysia.
In delivering the decision, Justice Mohamad Zabidin Diah said it was not safe to rely on DNA evidence that had been cited in the trial -- semen taken from the aide's body that the prosecutor alleged was Anwar's -- since it could have been contaminated.
The court was not willing to give a guilty verdict based on uncorroborated evidence, the judge said.
Anwar had repeatedly insisted he was innocent, saying that the trial was an attempt by the government to end his political career -- an assertion the government has consistently denied.
Investigators arrested Anwar in July 2008 -- four months after a loose coalition of opposition parties that he led won 82 of 222 parliamentary seats in elections. It was the second time in the country's history that the ruling party had failed to gain the two-thirds majority needed to amend the constitution.
Before the verdict, Anwar and his supporters had been pessimistic about how the trial would turn out.
"I don't believe that I'm going to get a fair trial," Anwar said in February 2010. "A fair trial would require a competent, independent judge to immediately strike out the charge because any sexual assault must have a case to support the charge."
The trial that ended Monday was the second time Anwar had faced sodomy charges in court.
Anwar was the heir apparent to former Prime Minister Mahathir Mohamad until 1998, when he was sacked and charged with corruption and sodomy.
Anwar spent six years in prison after being convicted on corruption charges in 1999 and on sodomy charges involving his wife's former driver in 2000.
Malaysia's highest court overturned the sodomy conviction and ordered him released from prison in 2004. However, the corruption verdict was never lifted, barring him from running for political posts until 2008.