A two-judge bench struck down a landmark Delhi High Court ruling in 2009 which found that section 377 of the Indian penal code prohibiting "carnal intercourse against the order of nature" infringed the fundamental rights of Indians.
The decision four years ago to decriminalise gay sex emboldened the still largely closeted homosexual community which has since campaigned publicly against widespread discrimination and ignorance.
But the Supreme Court bench, headed by G.S. Singhvi on his last day before retirement, found the High Court had overstepped its authority and that a law passed by the British in 1860 was still constitutionally valid.
"It is up to parliament to legislate on this issue," Singhvi said in the judgement which crushed the hopes of activists who had fought the case and led to tears outside the court in central New Delhi.
The 2009 ruling was strongly opposed by religious groups, particularly leaders of India's Muslim and Christian communities, who appealed to the Supreme Court and were delighted with Wednesday's outcome.
"Such a decision was totally unexpected from the top court. It is a black day for the community," Arvind Narayan, a lawyer of the Alternative Law Forum gay rights group, told reporters outside court.
"We are very angry about this regressive decision of the court."
India's law minister promised to review the ruling, but a new gay rights law that would have limited public support is unlikely to pass the dysfunctional national parliament before general elections next year.
"We have the right to make laws and we will exercise that. If the parliament runs we shall take up this issue," Law Minister Kapil Sibal told reporters.
Human rights 'body blow'
Amnesty International called the decision a "body blow to people’s rights to equality, privacy and dignity", while a legal challenge from gay rights groups is also possible.
"We will explore all options, probably look into the option of a review petition," T. Tandon, a lawyer appearing for Naz Foundation, a non-profit group that has spearheaded legal activism against the law.
"The movement of gay rights is so much stronger now. It is not 2001, it is 2013. You can't have a decision like this."
Religious groups who opposed the 2009 repeal hailed the reinstatement of the law, which is rarely invoked in practice.
Activists say police use it to harass and intimidate gays who can be threatened with sentences of up to life imprisonment.
"We know that homosexuality is against nature," general secretary of the All India Muslim Personal Law Board, Abdul Raheem Quraishi, told AFP. "It goes against all its laws and it is what led to the spread of HIV/AIDS."
The United Nations Development Programme on HIV/AIDS had argued in 2008 that decriminalising homosexuality would help India to combat the spread of HIV/AIDS which affects an estimated 2.5 million people here.
Gay sex has long been a taboo subject in conservative India, where homophobic tendencies abound and many still regard being gay as a mental illness.
The 2009 ruling was widely described as India's "Stonewall" moment in reference to protests in New York 40 years earlier after a police raid on a gay bar which helped launch the gay and lesbian rights movement.
In recent years, the community in India has raised its profile through gay pride marches, magazines and events which have encouraged many to come out of the closet.
But the country still has no major gay public figures in politics, entertainment or sport.
At a small gathering in Mumbai, about 50 activists waved rainbow flags and posed with a banner showing "377" with a red line through it.
"I came here expecting to celebrate. But the fight continues," said one 32-year-old woman who gave her name as Raj.
The Supreme Court ruling puts India back in the company of most nations in the Islamic world and many African countries which criminalise homosexuality. The only country in South Asia where gay sex is now legal is Nepal.
In most western countries, the debate about same-sex couples has shifted on to their rights to marry.
"The judgement is astounding," said Parmesh Shahani, author of "Gay Bombay: Globalization, Love and (Be)longing in Contemporary India."
"If you read the High Court judgement -- which was incredibly progressive and intelligently worded -- it said the state had no business in interfering with who private citizens choose to love."