MEXICO CITY—Three years after President Trump vowed to build a wall on the U.S. border with Mexico to stop illegal immigrants, a much more effective obstacle has emerged: Mexican President Andrés Manuel López Obrador.

Earlier this week, Mexico deployed hundreds of troops from its new National Guard to halt a caravan of some 2,000 Central American migrants who crossed a shallow river on Guatemala’s border with Mexico seeking to make their way to the U.S. Mexican television broadcast video of the guardsmen using plastic shields and tear gas to break up the migrant caravan.

On Thursday, a phalanx of guardsmen disbanded a group of some 800 migrants who had entered Mexico undetected and were advancing north. Video images posted on Twitter showed the federal agents stomping in lockstep toward the migrants. They pushed back the migrants with their plastic shields and encircled them using pepper spray amid shoving and yelling.

Most of the migrants in the caravan have been arrested and sent to crowded detention facilities, from where most of them will be deported to their home countries, a Mexican immigration official said.

The televised images brought home to Mexicans what a growing number of them see as an uncomfortable truth about Mr. López Obrador, who last month completed his first year in office. The charismatic, silver-haired leftist nationalist, once a passionate critic of Mr. Trump, has now become the main enforcer of his anti-immigration policies.