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 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 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.
Jessica Stern, the executive director of the New York-based rights group OutRight Action International, said the original law had reverberated far beyond India, including in countries where gay people still struggle for acceptance.
"The sodomy law that became the model everywhere, from Uganda to Singapore to the UK itself, premiered in India, becoming the confusing and dehumanizing standard replicated around the world,'' she said in a statement, saying "today's historic outcome will reverberate across India and the world.''
The court's ruling struck down the law's sections on consensual gay sex, but let stand segments that deal with such issues as bestiality.
Homosexuality has a tangled history in India, and some of Hinduism's most ancient texts are accepting of gay sex. But same-sex couples have also been harassed for centuries in many Indian communities, whether Hindu, Muslim or Christian.
Transgendered people known as "hijras,'' for example, have long been a common sight in India. But their treatment - both shunned as impure, and embraced for the belief that they can bring powerful blessings - reflects the complexities of gay life here.
Homosexuality has gained a degree of acceptance in deeply conservative India over the past decade, particularly in big cities. India now has openly gay celebrities, and some high-profile Bollywood films have dealt with gay issues. But many gay people still face isolation and persecution, and the court's ruling will do little to change life on the ground for millions of people.
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.
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,'' he said. (AP)