Chancellor Angela Merkel's conservative sister party has suffered huge losses in Bavaria's state election, raising new doubts about her ruling coalition.
The centre-right Christian Social Union (CSU) is set to lose its absolute majority in the state parliament, which it has dominated since 1957.
The Greens surged into second place, with nearly 17.5%.
The anti-immigration AfD is set to enter Bavaria's parliament for the first time, with most votes counted.
The CSU got just over 37% - that is, more than 10% down on its 2013 result.
It was also a bitter night for Germany's decades-old centre-left Social Democrats (SPD), who slumped to just 9.7%. They and the CSU are in a fragile coalition with Mrs Merkel's Christian Democrats (CDU).
The CSU appears to have suffered from the row over immigration policy earlier this year between Mrs Merkel and Horst Seehofer, the CSU leader and German interior minister.
He said it was "not a nice day" but added it was only "one side of the coin" as the vote gives "a clear mandate" allowing the CSU to form a new government.
There are signs that the CSU is leaning towards forming a Bavarian coalition with the right-leaning Free Voters (FW), a new independent party that won 11.6%. That would be a closer alignment than governing with the Greens.
Alternative for Germany (AfD) came fourth with about 10%. It means AfD will have seats in 15 of Germany's 16 state parliaments.
German commentators say the parties in Mrs Merkel's coalition will strive to avoid any further splits ahead of another big state election - in Hesse on 28 October, currently run by the CDU.
SPD leader Andrea Nahles blamed her party's poor performance in Bavaria on squabbling within the coalition.
Rightward shift backfires
By Jenny Hill, BBC News, Bavaria
It was a terrible night for Angela Merkel's Bavarian sister party.
The CSU has reigned supreme for decades in what is conservative country. But recent attempts to harden its tone and policies on migration, which included picking high-profile fights with Mrs Merkel, appear to have backfired.
It's bad news for Mrs Merkel too. Bavaria reflects what's happening at national level, the dwindling popularity of the traditional centre-right and centre-left parties.
Much may now depend on the results in Hesse in a fortnight. The CDU is also losing support. Its general secretary Annegret Kramp-Karrenbauer said that what had happened in Bavaria was a warning.
Bavaria key facts
- Nearly 13 million residents and the biggest by area of Germany's 16 federal states
- Its capital Munich is Germany's third-largest city, after Berlin and Hamburg
- Second-highest GDP out of 16 German states
- Historically conservative region, with strong Catholic and local traditions
- Industrial powerhouse: car and IT sectors especially strong, rich in family-run firms
What happened to the CSU?
AfD's success in Bavaria has not been as great as in eastern Germany but it appears to have taken large numbers of votes from the CSU.
But by echoing some of the AfD's hardline policies such as on migration, the CSU also seems to have lost the support of more moderate voters.
The CSU's Markus Söder, current Bavarian prime minister, said the projected result was painful but the party should learn from it.
"We have to analyse the changes taking place both in Bavaria and in society," he said.
"One of the most important jobs we now have is to ensure that this state is stable, governable, and remains as strong as it is now."
Some German commentators say the CSU now has an old-fashioned image for many voters.
The BBC's Jenny Hill in Bavaria says the election illustrates the complexity of the challenge faced by so many of Europe's large established parties.
It's not simply the rise of the far right, she says, but that voters are walking away in favour of smaller, newer movements.