Washington, D.C.: President Donald Trump backed off his plan to impose tariffs on all Mexican goods and announced via Twitter on Friday night that the United States had reached an agreement with Mexico to reduce the flow of migrants to the southwestern border.

Trump tweeted the announcement only hours after returning from Europe and following several days of intense and sometimes difficult negotiations between American and Mexican officials in Washington.

The president’s threat that he would impose potentially crippling tariffs on the United States’ largest trading partner and one of its closest allies brought both countries to the brink of an economic and diplomatic crisis — only to be yanked back from the precipice nine days later. The threat had rattled companies across North America, including automakers and agricultural firms, which have built supply chains across Mexico, the United States and Canada.

Businesses had warned that the tariffs would increase costs for American consumers, who import everything from cucumbers to refrigerators from Mexico, and prompt retaliation from the Mexican government in the form of new trade barriers that would damage the US economy.

But the trade war ended before it began, forestalling that economic reckoning and an intraparty war that Trump had created by threatening tariffs to leverage the immigration changes he demanded. That tactic had drawn stiff protests from Republicans, including many senators, who have long opposed tariffs and worried the measure would hurt American companies and consumers.

In an unusual show of force against their own party’s president, Republican senators had threatened to try to block the tariffs if Trump moved ahead with them, and had demanded a face-to-face meeting with the president before any action.

For Mexico, the president’s threat was a replay of past episodes in which Trump ranted about the country’s lack of immigration enforcement. This year, he threatened to shut down the entire southwestern border, backing off only after aides showed him evidence that Mexican authorities were taking aggressive action to stop migrants.

This time, Mexican authorities were under similar pressure to find something that would mollify Trump.

According to a US-Mexico Joint Declaration distributed late Friday, Mexico agreed to “take unprecedented steps to increase enforcement to curb irregular migration,” including the deployment of its national guard throughout the country to stop migrants from reaching the United States.

The declaration, distributed by the State Department, said Mexico had also agreed to accept an expansion of a Trump administration programme that makes some migrants wait in Mexico while their asylum claims are heard in the United States.

“The United States looks forward to working alongside Mexico to fulfil these commitments so that we can stem the tide of illegal migration across our southern border and to make our border strong and secure,” Secretary of State Mike Pompeo said in a statement.

But the declaration by the two countries included an ominous warning as well, stating that if Mexico’s actions “do not have the expected results,” additional measures could be taken. The declaration said the two countries would continue talking about other steps that could be announced within 90 days.

It was not immediately clear how quickly the agreement would be carried out and whether it would work to reduce the number of migrants flowing into the United States. But the outcome prevents a trade war that experts from both countries had warned could have been an economic catastrophe on both sides of the border.

Mexican officials, led by Foreign Minister Marcelo Ebrard, opened negotiations this week by pledging to send as many as 6,000 additional troops to Mexico’s border with Guatemala in the hopes of stemming the northward flow of migrants from Central America.

But during the negotiations in Washington, which were at times joined by Vice-President Mike Pence and Pompeo, US officials expressed frustration that their Mexican counterparts were not offering to do enough to satisfy Trump.

Trump administration officials demanded that Mexico support changes in asylum rules that would allow the United States to more readily reject asylum seekers from Guatemala, Honduras and El Salvador. Mexico has for years resisted such a demand out of concerns about the political and economic cost.

In the agreement reached on Friday, Mexico did not assent to those changes. Instead, its government agreed to increase participation in a Trump administration programme called the Migrant Protection Protocols, in which some migrants are sent to wait in Mexico while their asylum claims in the United States are processed.

Martha Barcena, the Mexican ambassador to the United States, wrote Friday evening on Twitter that her government had agreed to “strengthen measures for the application of its immigration law.”

In particular, she said Mexico would provide health, education and employment opportunities to migrants who are waiting in Mexico while they seek asylum in the United States.

That should allow the Trump administration to expand the Migrant Protection Protocols programme, though it was not immediately clear how the new Mexican promises of cooperation would radically change the situation that already exists.

Mexico had in the past resisted making a formal bilateral agreement about the programme but had informally agreed to provide similar assistance to the migrants while they waited in Mexico. There are about 8,000 migrants waiting there.

Civil rights groups in the United States have challenged the programme, saying that it violates the rights of the migrants who are sent to wait in Mexico by depriving them of access to lawyers who could help them with their claims. A federal judge initially blocked the programme, but an appeals court let the Trump administration move forward with it while the legal challenge was heard.

“The Trump administration announced that it intends to further expand its forced return to Mexico policy, which has been illegal since day one and has already proven to be a disaster,” Omar Jadwat, director of the American Civil Liberties Union’s Immigrants’ Rights Project, said Friday night. “We continue to press our legal challenge to the policy.”

Barcena also wrote that there would be renewed “cooperation for the development and prosperity of southern Mexico and Central America.” Mexico has long pushed for the United States to invest more in helping the region economically.

For President Andres Manuel Lopez Obrador of Mexico, the standoff over the tariffs came at a very bad time — with risks for his nation’s economy and his own political standing at home.

The Mexican economy is anaemic and slowing, and its currency has weakened. The dispute with Trump put the country’s most important trade relationship — a rare bright spot in an otherwise bleak landscape — in the cross hairs for something that has nothing to do with trade.

At the same time, Lopez Obrador, who was elected on a promise to transform Mexico, tackling poverty and inequality, has big, expensive ambitions.

He has doubled pensions for the elderly, granted stipends to unemployed youth, paid a princely sum to cancel a new airport project started by his predecessor and vowed to build a new oil refinery. More broadly, he has promised to rescue the ailing state-run oil company Petroleos Mexicanos, or Pemex, which has racked up an astounding $100 billion in debt.

The fight with his giant neighbour risked plunging his country into an economic crisis that would endanger those plans.

Before the deal was made, Lopez Obrador had called on Mexicans to rally Saturday in the border city of Tijuana to both defend the dignity of Mexico and, at the same time, celebrate its friendship with the United States. On Friday night, he tweeted that the rally would go on as planned.

The agreement that Trump announced ended — at least for now — an escalation of the trade wars the United States has waged against other countries, like China.

The new levies would have raised the cost of a wide array of products imported from Mexico, including cars, cucumbers, blue jeans, packaged food and chemicals. Trump had warned that he was prepared to ramp tariffs up each month until they hit 25 per cent in October.