“Migration policy is one reason for the upswing in the Greens,” who received votes from liberal conservatives appalled at how much their old party veered to the right during the campaign, Mr. Oberreuter said.

Forty percent of new Green supporters across Germany come from the center right of the political spectrum, according to research by the Forsa Institute.

At the same time, migration was also the reason a more nationalist faction inside the conservatives defected to the far right in the Bavarian vote. The far-right Alternative for Germany party was expected to win 10.3 percent of the vote, allowing it to enter the Bavarian Parliament for the first time.

Yet while the far right received far more attention during what was a noisy and at times nasty campaign, it was the Greens whose vote looked set to nearly double, to 17.2 percent, making the party the second-strongest in a region long considered one of Germany’s most conservative. They also came first in many of Bavaria’s biggest cities, including Munich, where they were on course to win more than 30 percent.

As Barbara Stamm, a veteran conservative lawmaker in Bavaria, put it on Sunday night, “You can’t win as many votes on the right as you lose in the middle.”

Over all, the Christian Social Union was on course to see its vote share slump to 37.5 percent, exit polls suggested. And Ms. Merkel’s other coalition partner, the Social Democrats, collapsed to a level of support lower than the far right’s, a result that some see as a harbinger of further losses for Ms. Merkel’s conservatives. Already at a postwar low, in two weeks her party is expected to lose ground — and possibly the election — in another regional ballot, in the state of Hesse.