Uganda's Constitutional Court has annulled tough anti-gay legislation signed into law in February.
It ruled that the bill was passed by MPs in December without the requisite quorum and was therefore illegal.
Homosexual acts were already illegal, but the new law allowed for life imprisonment for "aggravated homosexuality" and banned the "promotion of homosexuality".
Several donors have cut aid to Uganda since the law was adopted.
Ugandan government spokesperson Ofwono Opondo said the government was still waiting the attorney general's advice about whether to challenge the ruling in the Supreme Court.
He added that the ruling showed to Western donors that Uganda's democracy was functioning very well and that they should reinstate any aid they had cut.
The Ugandan authorities have defended the law in the past, saying President Yoweri Museveni wanted "to demonstrate Uganda's independence in the face of Western pressure and provocation".
'Null and void'
Uganda is a deeply conservative society where many people oppose gay rights and the sentence for homosexual acts has always been life imprisonment.
Earlier drafts of the anti-homosexuality act made it a crime not to report gay people - which would have made it impossible to live as openly gay - but this clause was removed.
However the legislation that was passed in parliament was "null and void", the presiding judge at the Constitutional Court said, as not enough lawmakers had been present to vote on the bill.
The law, which was signed by President Museveni in February, toughened up existing laws.
Lesbians were covered for the first time and those found living in a same-sex marriage could have been sentenced to life imprisonment.
The challenge to the law was brought by 10 petitioners, including academics, journalists, both ruling and opposition MPs, human rights activists and rights groups.
"The retrogressive anti-homosexuality act of Uganda has been struck down by the constitutional court - it's now dead as a door nail," the AFP news agency quotes prominent journalist Andrew Mwenda, one of the petitioners, as saying.
The BBC's Catherine Byaruhanga in the capital, Kampala, says supporters of the anti-gay laws have been angered by the ruling of the five judges.
They wonder whether their decision has anything to do with the president's visit to Washington next week for the US-Africa Summit, she says.
In June, the US imposed sanctions on the East African nation, including travel restrictions on Ugandan officials involved in serious human rights abuses.
The White House also cut funds to a number of programmes it is running with the Ugandan authorities.
Several European nations - including Denmark, Norway, the Netherlands and Sweden - had earlier cut aid.
Pastor Martin Ssempa, a vocal backer of the anti-homosexuality legislation, told the BBC his supporters would be asking parliament to investigate the impartiality of the judiciary.
Our reporter says if the state does not challenge the ruling it could re-table the bill in parliament where the ruling National Resistance Movement holds the majority in the house.
Uganda's anti-homosexuality act:
- Life imprisonment for gay sex, including oral sex
- Life imprisonment for "aggravated homosexuality", including sex with a minor or while HIV-positive
- Life imprisonment for living in a same-sex marriage
- Seven years for "attempting to commit homosexuality"
- Between five and seven years in jail or a $40,700 (£24,500) fine or both for the promotion of homosexuality
- Businesses or non-governmental organisations found guilty of the promotion of homosexuality would have their certificates of registration cancelled and directors could face seven years in jail.