MEXICO CITY—Mexico, a country racked by drug-related violence, took a major step toward fully legalizing marijuana on Wednesday, in the latest challenge to the U.S.-backed war on drugs in Latin America.

In a controversial ruling, Mexico’s Supreme Court decided in favor of four plaintiffs’ right to produce and consume marijuana for personal use, a move advocates hope will lead to the rapid legalization of recreational and medicinal marijuana nationwide.

“This...