The provision, known as Section 66A of the 2008 Information Technology Act, had made sending such messages a crime punishable by up to three years in prison.
In its ruling, the Supreme Court said the provision was "clearly vague" in not clarifying what should be construed as offensive. It also said the provision violates people's freedom of speech and their right to share information.
"The public's right to know is directly affected," the judges said in deeming the provision unconstitutional.
A law student who filed the challenge in 2012, Shreya Singhal, applauded the court's rejection of a provision she said was "grossly offensive to our rights, our freedom of speech and expression."
"Today the Supreme Court has upheld that, they have supported our rights," Singhal said. "I am ecstatic."
The law has been invoked in at least 10 recent cases, most often involving criticism of political leaders.
In 2012, a chemistry professor and his neighbour in Kolkata were arrested for forwarding a cartoon that made fun of West Bengal's top elected official, Mamata Banerjee.
Police arrested a man last year for saying on Facebook that Prime Minister Narendra Modi, then still a candidate, would start a holocaust in India if elected to office.
And last week, police in the northern state of Uttar Pradesh arrested a teenage student for posting comments on Facebook he attributed to a top state minister.
The student, jailed for two days before being released on bail, told reporters he was happy the provision was scrapped, though he was still recovering from "a very rough time."
Former finance and home minister P. Chidambaram welcomed the court's ruling, although his son had filed a police complaint in 2012 against a businessman for allegedly disparaging him in Twitter messages.
"The section was poorly drafted and was vulnerable," Chidambaram said of the law, which was passed while his Congress party was in power. "It was capable of being misused and, in fact, it was misused."
Cyber analysts said the ruling marked a positive step in ensuring that the Internet would be governed by the same norms and laws as newspapers, TV commentary and other forms of communication as India's Internet users increase from today's 100 million online.
"This sets the tone for the future of India's democracy and participation in this medium," said Samir Saran of the New Delhi think-tank , Observer Research Foundation. "It's the ethos around freedom of expression that is being reaffirmed. It tells us that arbitrary executive infringements of the constitution will be struck down."
He and other analysts said, however, that there was still more work to be done in guaranteeing the Internet was governed fairly, including a provision that allows the government to block websites without announcing or explaining its decision to do so. The Supreme Court on Tuesday upheld that part of the law.
"That's wrong. That's bad," Saran said, calling for a review to decide criteria for "why something should be blocked and when it should be blocked."
Follow Katy Daigle on Twitter: http://twitter.com/katydaigleSuggest a correction