Celebrations have erupted in India after the supreme court unanimously ruled to decriminalise homosexual sex in a landmark judgment for gay rights.

A five-judge bench at the country’s highest court ruled that a 160-year-old law banning sex “against the order of nature” amounted to discrimination on the basis of sexual orientation and was unconstitutional.

The judgment, after 24 years of legal challenges, triggered elation among LBGT Indians and their allies across the country and plans for all-night parties in nightclubs in major cities.

In Mumbai, people marched carrying a giant rainbow banner; in Bangalore they draped themselves in the LBGT flag and let off scores of balloons. In Delhi’s luxury Lalit hotel, run by one of the activists who fought Thursday’s case, and home to one of the city’s furtively gay-friendly nightclubs, staff danced in the lobby.

“Criminalising carnal intercourse under section 377 Indian penal code is irrational, indefensible and manifestly arbitrary,” said the chief justice, Dipak Misra, in his decision, announced at about 11.30am on Thursday.

Misra’s was one of four written judgments agreeing to scrap the ban. The rulings quoted Lord Alfred Douglas (“The love that dare not speak its name”), Leonard Cohen (“From the ashes of the gay/ democracy is coming”), William Shakespeare (“What’s in a name? That which we call a rose by any other name would smell as sweet”) and the German philosopher Johann Wolfgang von Goethe (“I am what I am, so take me as I am”).

Misra said: “Social exclusion, identity seclusion and isolation from the social mainstream are still the stark realities faced by individuals today, and it is only when each and every individual is liberated from the shackles of such bondage … that we can call ourselves a truly free society.”

Indian activists celebrate after the hearing. Photograph: Divyakant Solanki/EPA

Another judge on the bench, Indu Malhotra, said: “History owes an apology to members of the community for the delay in ensuring their rights.”

The judges accepted estimates that up to 8% of India’s population – 104 million people – might be LGBT, one of the largest Tsuch populations in the world.

The announcement of the decision drew loud cheers from a crowd gathered on a lawn outside the supreme court.

“It’s in our favour,” Smriti, 19, who did not give her last name, shouted as she embraced three others before they were mobbed by television cameras. “There’s so much work to be done, but it’s a great first step. We’re not criminals in our own country.”

Ritu Dalmia, one of the five LGBT campaigners who put their names to the legal petition that succeeded on Thursday, said the verdict made her feel hopeful.

“I was turning into a cynical human being with very little belief in the system, but honestly this has really shown once again that we are a functional democracy where freedom of choice, speech and rights still exist,” she said.

“Today is a historic day,” said Anand Grover, one of the lawyers who led the case. “The future is for everybody to be included, to realise their fundamental rights of equality, privacy, dignity et cetera. That is what the court has stated and given directions that this be made available and known to everybody.”

Show more

The decision appears to mark the end of a fraught path to legalising homosexuality in modern India. Cases filed in 1994 and 2001 bounced back and forth for years between courts reluctant to rule on the issue. The Delhi high court ruled against the ban in 2009 but was that overturned four years later by the supreme court.

Critics of the law say that although prosecutions under section 377 are rare, it was frequently used to blackmail gay and lesbian Indians and contributed to their marginalisation, while also inhibiting efforts to fight diseases such as HIV/Aids.

One LGBT group, the Humsafar Trust, said its crisis response team in Mumbai had attended to 18 cases in the past two years of gay men who were being blackmailed by the police or by people threatening to report them to authorities.

It said it had received at least 52 reports of LGBT people experiencing harassment or discrimination in the workplace who were unable to report it because of the ban on homosexuality.

Lawyers working to overturn the supreme court’s 2013 decision had a breakthrough last year. “What changed everything was last year’s privacy judgment,” said Gautam Bhatia, a Delhi-based lawyer and legal scholar. “In August 2017 the supreme court held there was a fundamental right to privacy, and as part of that, five judges said the 2013 decision was wrong.

“It was unprecedented. The judges commented on a completely unconnected case to say it was wrong. But once they said it, with the imprimatur of a full bench behind it, section 377 was gone, implicitly if not formally.”

Celebrations in Mumbai after the country’s top court struck down the colonial-era law. Photograph: Rafiq Maqbool/AP

When the petition was heard by the supreme court over four days in July, supporters were encouraged when India’s Hindu nationalist government, which had previously expressed support for section 377, chose not to file a submission.

Swami Agnivesh, a Hindu cleric who supported the abolition of section 377, said the Vedas, the scriptures that undergird many Hindu beliefs, contained nothing that barred same-sex relationships.

“According to the Vedas, all human beings constitute one family, irrespective of what country they belong to or their skin colour,” he said outside the supreme court. “If two adults decide to have according to their sexual orientation, to have a relationship in private, why should anyone have an objection?”

The decision legalises behaviour that many Indians say was accepted in their culture before the imposition of conservative Victorian mores during the British imperial era. The anti-sodomy law was imposed in the Indian colony as part of a raft of laws against public vice and immorality instituted across the British empire.

“I’m elated,” said Harish Iyer, a veteran gay activist and writer. “We have thrown out the British once again.”