Mexico's supreme court has decriminalised abortion nationwide.
The judgement comes two years after the court ruled in favour of a challenge to the existing law in the northern state of Coahuila. It had ruled that criminal penalties for terminating pregnancies were unconstitutional.
Mexico's states and the federal government had since been slow to repeal penal codes.
The new ruling legalises abortion across all 32 states.
The supreme court said the denial of the possibility of a termination violated the human rights of women.
"In cases of rape, no girl can be forced to become a mother - neither by the state nor by her parents nor her guardians," said the head of the supreme court, Arturo Zaldívar.
"Here, the violation of her rights is more serious, not only because of her status as a victim, but also because of her age, which makes it necessary to analyse the issue from the perspective of the best interests of minors."
The judgement opens the door for the federal healthcare system to provide abortions. It has been welcomed by women's rights groups.
Mexico City was the first of the country's states to decriminalise abortion in 2007 and a dozen others followed suit.
But in addition to a lack of facilities to carry out the procedure, "many women don't know that they have this right because local governments have not carried out publicity campaigns about it", women's rights activist Sara Lovera told AFP news agency.
"That's why today's decision of the Supreme Court is important."
The new ruling is likely to anger Mexico's more conservative politicians and the Catholic Church, in what is Latin America's second largest Catholic nation.
However, the Church's influence has been declining in recent years and the country's government considers itself staunchly secular.
Latin America has seen a trend towards loosening abortion restrictions that has been referred to as a "green wave".
Elective abortion is legal in Colombia, Cuba, Uruguay and Argentina though the frontrunner in the campaign for Argentina's presidential election in October, Javier Milei, wants to ban the procedure.
Some countries allow abortions in circumstances such as rape or health risks, while outright bans apply in El Salvador, Honduras, Nicaragua, Haiti and the Dominican Republic.
The reforms in Mexico and other Latin American countries contrast with the situation in the United States, where a Supreme Court ruling last year overturned the landmark 1973 Roe v. Wade decision guaranteeing the right to abortion nationwide.