The move was seen by immigration advocates as a hard-line deterrent that would force many of the migrants, even those fleeing violence and persecution, to return home. It could also give the United States a rationale to close ports of entry, the advocates said.

“I am surprised the new Mexican government would agree to carry Trump’s water on this, given his harsh rhetoric toward Mexicans,” said Kevin Appleby, the policy director of the Center for Migration Studies. “The administration will use this agreement moving forward to put up a virtual wall against asylum seekers. In some ways, Mexico will be paying for a wall.”

The new policy will most likely alleviate pressure on American border agents who for months have argued that they are overwhelmed by the record number of migrant families seeking asylum. United States Customs and Border Protection houses the families temporarily, usually for several days, while they are processed.

Concerns about the agency’s handling of children peaked this month, when a 7-year-old girl died in Border Patrol custody. The Trump administration did not take responsibility for the death, though days afterward, Kevin K. McAleenan, the commissioner of Customs and Border Protection, told Congress, “Our Border Patrol stations and ports of entry were built to handle mostly male, single adults in custody, not families or children.”

A senior Department of Homeland Security official said that Thursday’s announcement surprised many people in the agency’s leadership, as well as the rank-and-file employees who would be charged with carrying out the policy.

Many logistical concerns had yet to be addressed, the official said, and it was still unclear whether anyone might be exempt — such as children traveling alone.

Critics of the policy predicted that it would soon be challenged in court, on the grounds that it might violate Congress’s intention to allow asylum seekers into the United States. Additionally, the critics noted, the United States should adhere to international conventions that prohibit governments from returning refugees to places where they face a threat to their life or freedom.