TECUN UMAN, Guatemala — Migrants traveling in a mass caravan burst through a Guatemalan border fence and streamed by the thousands toward Mexican territory Friday, defying Mexican authorities’ entreaties for an orderly crossing and President Trump’s threats of retaliation.

On the Mexican side of a border bridge, they were met by a phalanx of police with riot shields. About 50 managed to push their way through before officers unleashed pepper spray and the rest retreated. The gates were closed again, and police used a loudspeaker to address the masses, saying, “We need you to stop the aggression.”

Mexican federal police chief Manelich Castilla, speaking from the border town of Ciudad Hidalgo, told Foro TV that his forces achieved their main objective of preventing a violent breach by the 3,000-plus migrants. In a separate interview with Milenio television, he accused people not part of the caravan of attacking police with firecrackers and rocks.

“It will be under the conditions that have been said since the start,” Castilla said. “Orderly, with established procedures, never through violence or force as a group of people attempted.”

The chaos calmed somewhat as migrants formed lines in a mass of humanity stretching across the bridge. Some returned to the Guatemalan side to buy water and food.

But others, tired of waiting, jumped off the bridge into the Suchiate River. Migrants organized a rope brigade to ford its muddy waters, and some floated across on rafts operated by local residents who usually charge a dollar or two to make the crossing.

Cristian, a 34-year-old cell phone repairman from San Pedro Sula, said he left Honduras because gang members had demanded protection payments of $83 a month, a fifth of his income. It was already hard enough to support his four daughters on the $450 he makes, so he closed his small business instead.

Cristian, who declined to give his last name because the gangsters had threatened him, estimated that about 30 percent of the migrants want to apply for refugee status in Mexico, while the rest want to reach the United States.

“I want to get to the States to contribute to that country,” Cristian said, “to do any kind of work, picking up garbage.”

Police and immigration agents let small groups of 10, 20, 30 people through the gates if they wanted to apply for refugee status. Once they file a claim, they can go to a shelter to spend the night.

Eric Lagos Rodriguez from Tegucigalpa, the Honduran capital, and his family turned themselves over to authorities to apply.

“We couldn’t go on like this,” Lagos said. “We’re traveling with six children.”

As dusk neared, police were relieved by fresh officers and reformed ranks. Migrants continued to hang on the gates, yelling “there are children here” and “we are hungry.” Back on the Guatemalan side, some people set up tarp shelters.

Migrants who want to apply for refuge in Mexico were welcome to do so, officials said, but any who decide to cross illegally and are caught will be detained and deported.

Secretary of State Mike Pompeo met Friday with President Enrique Pena Nieto and Foreign Relations Secretary Luis Videgaray in Mexico City, with the caravan high on the agenda.

At a news conference with Videgaray, Pompeo called illegal migration a “crisis” and emphasized “the importance of stopping this flow before it reaches the U.S. border,” while also acknowledging Mexico’s right to handle the crisis in a sovereign fashion.

On Thursday, Trump threatened to close the U.S. border if Mexico didn’t stop the caravan. Later that day, he tweeted a video of Mexican federal police deploying at the Guatemalan border and wrote: “Thank you Mexico, we look forward to working with you!”

Migrants have banded together to travel en masse regularly in recent years, but this caravan was unusual for its huge size, said Victor Clark Alfaro, a Latin American studies professor at San Diego State University. By comparison, a caravan in April that also attracted Trump’s ire numbered about 1,000.

“It grabs one’s attention that the number of people in these kinds of caravans is on the rise,” Clark Alfaro said. “It is migration of a different dimension.”