India's Supreme Court has ruled that gay sex is no longer a criminal offence.
The ruling overturns a 2013 judgement that upheld a colonial-era law, known as section 377, under which gay sex is categorised as an "unnatural offence".
The court has now ruled discrimination on the basis of sexual orientation is a fundamental violation of rights.
Campaigners outside the court cheered and some broke down in tears as the ruling was handed down.
Although public opinion in India's biggest cities has been in favour of scrapping the law, there remains strong opposition to homosexuality among religious groups and in conservative rural communities.
But this ruling from the top court is the final say in the matter and represents a huge victory for India's lesbian, gay, bisexual and transgender (LGBT) communities.
One activist outside the court told the BBC: "I hadn't come out to my parents until now. But today, I guess I have."
The unanimous decision was delivered by a five-judge bench, headed by India's outgoing chief justice Dipak Misra.
"Criminalising carnal intercourse is irrational, arbitrary and manifestly unconstitutional," Mr Misra said.
Another judge, Indu Malhotra, said "history owes an apology" to LGBT people for ostracising them.
Justice DY Chandrachud said the state had no right to control the private lives of LGBT community members.
Denying the right to sexual orientation was the same as denying the right to privacy, Mr Chandrachud said.
The ruling effectively allows gay sex among consenting adults in private.
The overturned law, section 377, was a 157-year-old colonial-era law, which criminalised certain sexual acts that were punishable by a 10-year jail term.
The law punished "carnal intercourse against the order of nature with any man, woman or animal".
While the statute criminalised all anal and oral sex, it largely affected same-sex relationships.
Human rights groups say police have used the statute to harass and abuse members of the LGBT communities.
Campaigners are describing the verdict as a "new dawn for personal liberty".
But in a largely conservative India, where leaders of all religions have consistently opposed gay sex, it will still be a while before attitudes change and the community finds full acceptance.
But laws almost always play an important role in changing mindsets, and by recognising the community's right to love, the Supreme Court has restored the dignity denied to them for a very long time.
A bid to repeal section 377 was initiated in 2001 and was batted between court and government until 2009, when the Delhi High Court ruled in favour of decriminalisation.
Several political, social and religious groups then mobilised to restore the law and in 2013 the Supreme Court struck down the High Court ruling.
Anti-section 377 activists then submitted a formal request to review an earlier court order - and in 2016 the Supreme Court decided to revisit its ruling.
Equal rights activists have argued that the very existence of such a law was proof of discrimination based on sexual orientation.
LGBT activist Harish Iyer told the BBC was thrilled with the court decision.
"I'm absolutely elated. It's like a second freedom struggle where finally we have thrown a British law out of this country... I think the next step would be to get anti-discrimination laws in place, or anti-bullying laws."
The governing BJP party has said it would leave the decision to the Supreme Court.
However, one of its members said he was disappointed with the verdict.
Subramanian Swamy, known for making provocative comments, said: "It could give rise to an increase in the number of HIV cases."
Meanwhile, the main opposition Congress party has welcomed the ruling, saying they "hope this is the beginning of a more equal and inclusive society".
The UN has also welcomed the ruling, saying "sexual orientation and gender expression form an integral part of an individual's identity the world over".
Author and commentator Sandip Roy told the BBC that although the ruling was a cause for celebration, there were still hurdles to overcome, and a need for anti-discrimination laws.
"I think we would be foolish to think that this is the end of the fight," he said.
Homosexuality remains illegal in many parts of the world. A 2017 report from the International Lesbian, Gay, Bisexual, Trans and Intersex Association (ILGA) lists 72 countries and territories where same-sex relationships are still criminalised, although that included India before its latest ruling.
Most of them are in Africa, the Middle East and other parts of south Asia.
The ILGA report said homosexuality could still result in the death penalty in eight nations.
In many places, breaking laws against homosexuaity could be punishable by long prison sentences, fines or corporal punishment.
India's landmark judgment sparked celebrations not only across India, but elsewhere in South Asia, where activists hope to push for similar reform.
Activists in Muslim-majority Bangladesh and Pakistan said they planned to push for reform of the laws that their countries also inherited from colonial Britain.
"The Bangladeshi LGBT community has gained moral support," said Shahanur Islam, executive director of the Bangladesh Institute for Human Rights.
"We hope and will make sure that other countries will follow suit in overturning this remnant from colonial law," said Mani Aq of the Pakistani branch of the Naz Foundation.
In a number countries, legal challenges could bring about change:
- In Kenya, the high court is due to rule on whether to decriminalise homosexuality
- A separate case in Kenya is challenging restrictions preventing LGBT groups from setting up non-governmental organisations
- In Botswana, a decriminalisation case is awaiting the appointment of a judge
- In Jamaica, laws criminalising gay relations are facing a challenge.
Many of the states that criminalise homosexual relations are Commonwealth countries with legal statutes originating from British colonial times.
That makes the ruling in India striking down a colonial-era law particularly significant, says Tea Braun of the Human Dignity Trust, a UK-based charity that supports those challenging anti-gay laws.
"Its dismantling by the Indian Supreme Court, after over a century and a half of oppression against LGBT people, signals a change in tide for the world," she told the BBC.
Same-sex marriage is now legal in 25 countries worldwide, says ILGA.
In August 2018, Costa Rica's top court overturned a ban on same-sex marriage, and gave legislators 18 months to change the law.
A further 28 countries guarantee some civil-partnership recognition, according to the ILGA.
- BBC/ Reuters