The legislation sets life imprisonment as the penalty for gay sex involving an HIV-infected person, acts with minors and the disabled, as well as repeated sex offenses among consenting adults, according to the office of a spokeswoman for Uganda's parliament.
The bill also prescribes a seven-year jail term for a person who "conducts a marriage ceremony" for same-sex couples.
When the bill was first introduced in 2009, it was widely condemned for including the death penalty, but that was removed from the revised version passed by parliament.
President Yoweri Museveni must sign the bill within 30 days for it to become law. Although in the past he spoke disparagingly of gays, in recent times Museveni has softened his position, saying he is only opposed to gays who appear to "promote" themselves.
"In our society there were a few homosexuals," Museveni said in March. "There was no persecution, no killings and no marginalization of these people but they were regarded as deviants. Sex among Africans, including heterosexuals, is confidential. If I am to kiss my wife in public, I would lose an election in Uganda."
"It will open a new era of fear and persecution," he said. "If this law is signed by President Museveni, I'd be thrown in jail for life and in all likelihood killed."
The bill was denounced by rights groups in the United States.
The bill targets gays "with brutal persecution and is one of the worst human rights violations of our time," said Ross Murray, director of news for the American activist group, GLAAD. Murray said U.S. evangelical preachers and groups "created this travesty of justice, and it is now up to fair-minded Americans to speak out for the very lives of LGBT people in Uganda."
Human Rights First called on President Barack Obama to press Museveni not to sign the bill into law.
Homosexuality was already illegal in Uganda under a colonial-era law that criminalized sexual acts "against the order of nature," but the Ugandan lawmaker who wrote the new legislation argued that tougher legislation was needed because homosexuals from the West threatened to destroy Ugandan families and were allegedly "recruiting" Ugandan children into gay lifestyles.
Ugandan gays disputed this account, saying that Ugandan political and religious leaders had come under the influence of American evangelicals who wanted to spread their anti-gay campaign in Africa. Ugandan gays singled out Scott Lively, a Massachusetts evangelical, and sued him in March 2012 under the Alien Tort Statute that allows non-citizens to file suit in the United States if there is an alleged violation of international law. Rejecting Lively's request to dismiss the lawsuit, a federal judge ruled in August the case could proceed, saying systematic persecution on the basis of sexual orientation violates international norms.
Lively denied he wanted severe punishment for gays, and has previously told The Associated Press he never advocated violence against gays but advised therapy for them.
Ugandan gays had believed progress was being made to strengthen their rights in a country where prejudice against homosexuals is rampant. In 2012 they held their first gay pride parade and have sometimes joined street marches in support of all human rights.
Despite criticism of the anti-gay legislation abroad, it is highly popular among Ugandans who say the country has the right to pass laws that protect its children.
Amid international criticism, the bill was repeatedly shelved despite the protests of Ugandan lawmakers. Days before Christmas last year, the speaker of Uganda's parliament, Rebecca Kadaga, said the anti-gay legislation would be passed as a "Christmas gift" to Ugandans. She presided over the session Friday that passed the bill despite opposition from Ugandan Prime Minister Amama Mbabazi, who wanted the vote delayed.
David Bahati, the lawmaker who wrote the bill, said in a Facebook update Friday that the legislation was necessary "to defend our culture and to defend the future of our children."
When the bill was first proposed, Obama called it "odious."
Maria Burnett, a senior Africa researcher with Human Rights Watch, said the bill passed Friday is "still appalling" despite some amendments.
Homosexuality remains a taboo subject across many parts of Africa. Some 38 African countries — about 70 percent of the continent — criminalize homosexual activity, Amnesty International said in a report released earlier this year.
The rights group said of the new Ugandan law that it "would significantly hamper the work of human rights defenders and others who find themselves in conflict with the law merely by carrying out their legitimate activities."