NEW DELHI — India’s Supreme Court on Wednesday struck down a part of the country’s legal code that had permitted men to have sex with their underage wives — a decision that human rights groups said was an important step forward for the rights of girls.

Indian law sets 18 as the age for marriage and consent to sex for a young woman. But another provision of the law was inconsistent, saying a man could have sex with a girl as young as 15, as long as she was his wife.

Advocates argued to the court that this exception encouraged child marriage, which is prevalent throughout rural India and has imperiled many development goals, like improving education, ending poverty and reducing family size.

It has also put millions of Indian girls in dangerous situations where they are abused.

Wednesday’s ruling ended the legal discrepancy. The court said, “Sexual intercourse with a girl below 18 years of age is rape regardless of whether she is married or not.”