Same-sex relations were already illegal in Uganda, as in more than 30 African countries, but the new law goes further.
It stipulates capital punishment for "serial offenders" against the law and transmission of a terminal illness like HIV/AIDS through gay sex. It also decrees a 20-year sentence for "promoting" homosexuality.
"The Ugandan president has today legalised state-sponsored homophobia and transphobia," said Clare Byarugaba, a Ugandan rights activist.
United States President Joe Biden called the move "a tragic violation" of human rights and said Washington would evaluate the implications of the law "on all aspects of US engagement with Uganda."
"We are considering additional steps, including the application of sanctions and restriction of entry into the United States against anyone involved in serious human rights abuses or corruption," he said.
A presidency photo of Museveni showed him signing the law with a golden pen at his desk. The 78-year-old has called homosexuality a "deviation from normal" and urged lawmakers to resist "imperialist" pressure.
A local organisation, Human Rights Awareness and Promotion Forum, and 10 other individuals later filed a complaint against the law at the constitutional court, one of the petitioners, Busingye Kabumba, told Reuters.
Museveni had sent the original bill passed in March back, asking parliament to tone down some provisions. But his ultimate approval was not seen as in doubt in a conservative country where anti-LGBTQ attitudes have hardened in recent years, in part due to campaigning by Western evangelical church groups.
Uganda receives billions of dollars in foreign aid each year and could now face adverse measures from donors and investors, as happened with a similar bill nine years ago.
The bill's sponsor, Asuman Basalirwa, told reporters that parliament speaker Anita Among's US visa was cancelled after the law was signed. Among and the US embassy in Uganda did not immediately respond to requests for comment.
In a joint statement, the US's flagship HIV/AIDS programme PEPFAR, the Global Fund to Fight AIDS, Tuberculosis and Malaria, and the Joint United Nations Programme on HIV/AIDS (UNAIDS) said the law put Uganda’s anti-HIV fight "in grave jeopardy".
Dominic Arnall, chief executive of Open For Business, a coalition of companies that includes Google and Microsoft, said the group was deeply disappointed and the law ran counter to Ugandans' economic interests.
The UN human rights body declared itself "appalled".
Uganda's move could encourage lawmakers in neighbouring Kenya and Tanzania seeking similar measures.
"What a leader we've in Africa!" tweeted George Kaluma, a Kenyan member of parliament who submitted an anti-LGBTQ bill in April.
"Kenya is following you in this endeavour to save humanity."
The inclusion of the death penalty for offences like transmitting HIV has drawn particular outrage internationally.
Existing Ugandan law calls for a maximum 10-year sentence for intentionally transmitting HIV and does not apply when the person who contracted the infection was aware of their sexual partner's HIV status.
By contrast, the new law makes no distinction between intentional and unintentional transmission and contains no exception based on awareness of HIV status.
The amended version of the bill, adopted earlier this month after Museveni returned it to parliament, stipulated that merely identifying as LGBTQ is not a crime and revised a measure that obliged people to report homosexual activity to only require reporting when a child is involved.
LGBTQ Ugandans called those changes useless, saying law enforcement regularly exceeds its legal authorities to harass them. They said passage of the bill in March unleashed a wave of arrests, evictions and mob attacks.
The issue has been a long-running one in Uganda.
A less restrictive 2014 anti-LGBTQ law was struck down by a Ugandan court on procedural grounds, after Western governments had initially suspended some aid, imposed visa restrictions and curtailed security cooperation.
In 2009, a bill dubbed "kill the gays" for initially proposing executing homosexuals was introduced after a conference in Kampala drew representatives from the United States including prominent anti-gay evangelical Scott Lively.
As well as religious campaigning, Africa's anti-LGBTQ attitudes also have their roots in the colonial era, including an anti-sodomy section of Britain's penal code. By the time the UK legalised same-sex acts in 1967, many former colonies were independent and did not inherit the legal change.
"To reduce any kind of human being, irrespective of their sexuality, to a death sentence based on who they identify as and how they choose to live their lives is something that we should all feel very ashamed about as a continent," said South African filmmaker Lerato.
"We can liken this to apartheid if not worse."
Reuters, the news and media division of Thomson Reuters, is the world's largest multimedia news provider, reaching billions of people worldwide every day.Go to: https://www.reuters.com/