India's Supreme Court on Thursday struck down a colonial-era law that made gay sex punishable by up to 10 years in prison, a landmark victory for gay rights that one judge said would "pave the way for a better future."

The 1861 law, a relic of Victorian England that hung on long after the end of British colonialism, was a weapon used to discriminate against India's gay community, the judges ruled in a unanimous decision.

"Constitutional morality cannot be martyred at the altar of social morality," Chief Justice Dipak Misra said, reading out the verdict. "Social morality cannot be used to violate the fundamental rights of even a single individual."

As the news spread, the streets outside the courthouse erupted in cheers as opponents of the law danced and waved flags.

"We feel as equal citizens now," said activist Shashi Bhushan. "What happens in our bedroom is left to us."

In its ruling, the court said sexual orientation was a "biological phenomenon" and that any discrimination on that basis violated fundamental rights.

"We cannot change history but can pave a way for a better future," said Justice D.Y. Chandrachud.

The law known as Section 377 held that intercourse between members of the same sex was against the order of nature. The five petitioners who challenged the law said it was discriminatory and led to gays living in fear of harassment and persecution.

Arvind Datar, the attorney for the petitioners, argued in the court that the provision was unconstitutional because it provides for the prosecution and sentencing of consenting adults.

Homosexuality has a tangled history in India, with some of Hinduism's most ancient texts accepting of gay sex. Transgendered people known as "hijras" have been a common sight in India for centuries. They are shunned by the wider community and often forced to work as beggars and prostitutes, but are also sometimes embraced because they are believed to bring powerful blessings.

On Thursday, a leader of a prominent hard-line Hindu group noted that while it doesn't see homosexuality as a crime, it believes gay marriage is not "compatible with nature."

Arun Kumar, a spokesman for the Rashtriya Swayamsevak Sangh, the parent organization of the ruling Bharatiya Janata Party, said Indian society "traditionally does not recognize" gay relationships, the Press Trust of India news agency reported.

A New Delhi High Court in 2009 declared Section 377 unconstitutional, but that decision was overturned in a ruling by three Supreme Court justices in 2013 on the grounds that amending or repealing the law should be left to Parliament. But lawmakers failed to take action and in July the government told the Supreme Court to give a ruling in the case.

Over the past decade, gays have gained a degree of acceptance in parts of deeply conservative India, especially in big cities. Some high-profile Bollywood films have dealt with gay issues. Still, being gay is seen as shameful in much of the country.

Sukhdeep Singh, a gay rights activist and editor of Gaylaxy Magazine, said the community still had a lot of distance to go "to be legally with your partner."

"This will obviously open the doors for a lot of more things, more civil rights. And we'll fight for our rights, definitely. This is the first battle that has been won and there are many more battles that we are going to fight and we'll win that as well. For sure," Mr. Singh said.

Karan Johar, a Bollywood producer and director, said Thursday's verdict was history in the making.

Get the Monitor Stories you care about delivered to your inbox. By signing up, you agree to our Privacy Policy

"So proud today! Decriminalizing homosexuality and abolishing section 377 is a huge thumb up for humanity and equal rights! The country gets its oxygen back!" Mr. Johar wrote on Twitter.

This story was reported by The Associated Press.