Under the deal, migrants who have already applied for asylum in other European Union countries will be held in transit centres
German Chancellor Angela Merkel has agreed to severely tighten German border controls in order to settle a row over migration that was threatening to topple her fragile governing coalition.
Emerging after five hours of talks late on Monday evening, Horst Seehofer, leader of Bavaria's Christian Social Union (CSU) and Germany's minister of the interior, told reporters he would remain in his post after a deal with Merkel's Christian Democrats (CDU) that he said would stem illegal immigration.
Seehofer had previously threatened to resign, arguing that Merkel's migration policy was too lenient and that migrants should be turned away at the Bavarian border if they had sought asylum elsewhere.
Both sides hammered out "a truly good compromise ... after a tough struggle and difficult days," Merkel said, adding that it involved setting up holding and processing centres for asylum seekers near German borders.
"We have reached an agreement after very intense negotiations," Seehofer agreed, stressing that he intended to stay on in his cabinet post and gloating that "it's worth fighting for your convictions".
"We now have a clear agreement how to prevent illegal immigration across the Austrian-German border in future," said Seehofer, whose CSU is the traditional Bavaria state ally of Merkel's CDU party.
READ MORE ►
The deal still requires the consent of Merkel's other coalition partner, the centre-left Social Democrats, to become government policy.
Anti-immigration parties are gaining ground across Europe. In Germany, the fierce debate over this deal has emboldened the far-right Alternative for Germany (AfD) party, which now threatens to unseat the CSU in October's regional elections.
Migrants to be detained
Under the deal, migrants who have already applied for asylum in other European Union countries will be held in transit centres on the border while Germany negotiates bilateral deals for their return.
It drew immediate fire from critics, such us Bernd Riexinger of the left-wing opposition party Die Linke, who on Twitter slammed the plan for "mass internment camps" and judged that "humanity got lost along the way".
The euro currency wobbled on several occasions during the weeks-long row, which threatened to break a 70-year-old alliance between the CSU and the Christian Democrats.
It was the latest aftershock from Merkel's 2015 decision to open Germany's borders to more than a million refugees from wars in the Middle East and Africa.
The transit centres, like "airside" zones in international airports, will be regarded for immigration purposes as not being in Germany, making it easier to deport refugees from them.
The compromise deal meant that Seehofer was able to hail tighter immigration controls, while Merkel was able to say that Germany was adhering to EU rules and maintaining - in principle - freedom of movement within the bloc.
"The spirit of partnership in the European Union is preserved and at the same time an important step to order (has been taken)," Merkel told reporters.
For now, the deal suggests that Merkel - in power for over 12 years, and the EU's longest serving leader - will go on to fight another day.