BERLIN — Chancellor Angela Merkel avoided the breakup of her government on Monday, emerging from an immigration fight with her coalition intact but fragile.

A clash that lasted weeks ended in an uneasy truce when Merkel and her party, the Christian Democrats (CDU), reached a compromise on immigration policy with partner Christian Social Union (CSU). Interior Minister and CSU leader Horst Seehofer backed off his threat to resign and potentially bring down the government. A hard-line stance on immigration and criticism of Merkel’s decision to welcome nearly a million migrants in 2015 have been part of the CSU’s political strategy ahead of regional elections in the fall.

The text, released in simple bullet points to be further negotiated in the days ahead, was a testament to how far Merkel and the German government have come from the “Willkommenskultur” (“welcoming culture”) she preached in 2015.

The deal proposes new screening at the Germany-Austria border to “prevent asylum seekers whose asylum procedures are the responsibility of other E.U. countries from entering the country.” A network of “transit centers” will serve as processing points from which ineligible migrants would then be sent back to relevant countries, but only if those countries consent. If those countries do not agree, Germany’s rejected migrants would be sent to Austria, “on the basis of an agreement.”

Merkel was widely seen here as handling the rebellion well. “She will prove to be a stable lighthouse in a tumultuous sea,” said Nils Diederich, a political-science professor at the Free University Berlin.

But the internal turbulence has preoccupied Merkel at a time when Germany is reckoning with far bigger international challenges, especially the growing transatlantic acrimony between Berlin and the Trump administration.

And the problems within her government are unlikely to go away: Relations between Merkel’s CDU and its Bavarian sister have been badly strained, and the CSU is riven by competing factions. That will make it difficult for her to return Germany to the relatively stable state for which it had long been known during her nearly 13-year tenure.

Merkel hailed the compromise as one that “preserves the spirit of partnership in the European Union and at the same time is a decisive step towards organizing and controlling secondary migration.”

Seehofer said: “I am glad that this agreement has been reached. It has once again become clear that it is worth fighting for a conviction. And what follows now is a very sustainable and clear agreement for the future.

It was not immediately clear the extent to which Austria’s right-wing government, which has opposed Europe’s migration wave, was consulted in these discussions.

The compromise also needs approval by Merkel’s other coalition partner: Germany's Social Democratic Party (SPD), which might chafe at the notion of migrant centers or camps along the border but which also stands to lose in any new elections that might be triggered by rejecting the compromise. The CDU, the CSU and the SPD began meeting after Merkel and Seehofer finished their negotiations.

Diederich noted that the CSU’s motivation in precipitating the crisis likely stemmed from its interest in fending off the far-right, anti-immigrant Alternative for Germany (AfD) in October’s elections. “What’s happening now has nothing to do with content,” Diederich said. “It’s solely aimed at the upcoming Bavarian state elections.”

That strategy hasn’t been successful, according to recent polls in Bavaria, which show a 1.9 percent drop in support for the CDU/CSU coalition, compared with a 2.4 percent rise in support for the AfD.

By Monday morning, there were already signs of regret from some CSU officials. Markus Söder, the Bavarian state premier — as well as Seehofer’s rival in the party — told reporters he was “very surprised” about Seehofer’s offer to step down. “One thing is very clear: The stability of the government is not a question for us,” he told German news services Monday. “One can reach a lot within a government, but not outside of it.”

The real victor of the fight could well be the AfD. Its leaders framed the standoff as a confirmation of its criticism of the political establishment. The party’s harsh anti-migrant stances, once banished to the fringes of public discourse, have entered the conservative mainstream.

“It’s a process of self-delegitimization that’s going on among the establishment,” said Michael Koss, a political scientist at Ludwig-Maximilians-University Munich, referring to the spat inside the coalition. “The AfD always claimed from the outside that the establishment politicians were only out for their own good, and currently I have to say I agree.”

The AfD wasted no time attacking Seehofer’s dramatics Monday morning. “Horst Seehofer and the CSU have been staging a miserable and slimy theater,” Alice Weidel, the AfD’s leader, posted on Facebook. “The back and forth and the resignation from the resignation of the interior minister were merely staged.” She then declared: “The AfD is the only power that can bring this asylum chaos under control.”

In fact, the number of asylum seekers in Germany has fallen dramatically to pre-2015 levels, thanks in part to Merkel’s more restrictive policies in recent years.

Whether the AfD can continue to capitalize on the coalition’s fissures remains to be seen, but what seems most certain is that in trying to gain back voters from the AfD and challenge Merkel, Seehofer miscalculated.

Merkel, who has been Germany’s chancellor since 2005, has often thwarted opponents by assuming their positions. But on migration — although some on the political left have criticized her for making too many concessions — she has refused to call the 2015 influx a mistake or to adopt the anti-migrant rhetoric of her more hard-line allies.

“She’s in the final phase of her political career. She aims to be remembered as a great European, or the honorable grandchild of Helmut Kohl,” said Werner Weidenfeld, a German political scientist. “That’s why this political debate has a different dimension.”

Political opponents wondered aloud how long the truce would endure.

“How often have #Merkel and #Seehofer said they’ve found a ‘good compromise’ that then resulted in an escalation just a few hours / days / weeks later?” tweeted Katrin Göring-Eckardt, the leader of the Green Party. “It’s exhausting. It doesn’t solve any problems, it just creates new ones.”

Griff Witte in Washington contributed to this report.

Correction: A previous version of this story used the abbreviation SDP in reference to Germany’s Social Democratic Party. The correct abbreviation is SPD.