In a unanimous decision, five Supreme Court justices ruled that the law was a weapon used to harass members of India's gay community and resulted in discrimination. After the ruling, opponents of the law danced and waved flags outside the court.
"We feel as equal citizens now," activist Shashi Bhushan said. "What happens in our bedroom is left to us."
The law, known as Section 377 - put in place by the British in 1861 - 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 being harassed and prosecuted by police.
Arvind Datar, the attorney for the petitioners, argued in the court that the penal provision was unconstitutional because it provides for the prosecution and sentencing of consenting adults.
In its ruling Thursday, the court said it must protect an individual's right to live with dignity as a fundamental right. The justices termed sexual orientation as a "biological phenomenon" and said any discrimination on this ground violated fundamental rights.
Chief Justice Dipak Misra said the denial of self-expression was akin to inviting death, 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.
Karan Johar, a Bollywood producer and director, said Thursday's verdict was history in the making.
"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!" he wrote on Twitter.
Justices side with Colorado baker on same-sex wedding cake
The Supreme Court ruled narrowly Monday for a Colorado baker who wouldn't make a wedding cake for a same-sex couple. But the court is not deciding the big issue in the case, whether a business can invoke religious objections to refuse service to gay and lesbian people.