BERLIN — Chancellor Angela Merkel, who staked her legacy on welcoming hundreds of thousands of migrants into Germany, agreed on Monday to build border camps for asylum seekers and to tighten the border with Austria in a political deal to save her government.

It was a spectacular turnabout for a leader who has been seen as the standard-bearer of the liberal European order but who has come under intense pressure at home from the far right and from conservatives in her governing coalition over her migration policy.

Although the move to appease the conservatives exposed her growing political weakness, Ms. Merkel will limp on as chancellor. For how long is unclear. The nationalism and anti-migrant sentiment that has challenged multilateralism elsewhere in Europe is taking root — fast — in mainstream German politics.

Ms. Merkel agreed to the latest policy after an insurrection over migration policy led by her interior minister, Horst Seehofer, threatened to bring down her coalition.