BERLIN — A far-right party fiercely opposed to Chancellor Angela Merkel’s welcome for refugees made startling gains in three state elections in Germany on Sunday, dealing the chancellor a blow as she tries to seal a deal with Turkey to reduce the influx of migrants.

In elections that showed how strongly the refugee crisis has scrambled politics and daily life in Germany, Ms. Merkel’s center-right Christian Democrats failed to wrest control of two states in western Germany where they had once been expected to do so.

In the one eastern state that voted, her party finished first. But the Alternative for Germany, a populist, nationalist party formed in 2013, was only five percentage points behind.

Ms. Merkel, now facing the toughest challenges of her political career, had no immediate comment on Sunday. She left that to party lieutenants on television talk shows that spent hours dissecting the muddled outcome of the first big electoral test of Ms. Merkel’s refugee policy.