German Chancellor Angela Merkel has accepted responsibility for her Christian Democratic party's "bitter defeat" in Berlin state elections.
She voiced regret over mistakes that contributed to last summer's migrant crisis in Germany. More than a million migrants reached Germany - a record.
"If I could, I would turn back time for many, many years, to prepare better," she told reporters.
Her CDU party can no longer run Berlin with the Social Democrats (SPD).
The centre-right CDU won 17.6% of the vote - its worst-ever result in Berlin.
Mrs Merkel conceded that her open-door policy towards migrants - embodied in her phrase "wir schaffen das" (we can manage it) - was a factor in the election. She has now distanced herself from that phrase, calling it "a sort of simplified motto".
She has been widely criticised in Germany for the policy, which was a humanitarian gesture faced with the desperate plight of migrants, many of them refugees from the war in Syria.
The right-wing, anti-migrant party Alternative for Germany (AfD) will enter the Berlin state parliament for the first time with 14% of the vote.
The AfD is now represented in 10 of Germany's 16 regional parliaments. Earlier this month it pushed the CDU into third place in the northern state of Mecklenburg-Western Pomerania.
Looking back at the migrant crisis, Ms Merkel defended her policy as "absolutely correct on balance, but ultimately it meant that for a long time we did not have enough control".
"Nobody wants a repeat of that situation - including me."
She said she needed to work harder to explain her migrant policies.
The SPD emerged as the strongest party with about 22%, in spite of losing almost 7% of its voters, and said it would hold talks on forming a coalition with all parties except AfD. It is expected to drop the CDU as a coalition partner in favour of the left-wing Die Linke and the Greens.
Sunday's election in Berlin, a city-state of 3.5 million people, was dominated by local issues including poor public services, crumbling school buildings, late trains and a housing shortage, as well as problems in coping with the migrant influx.
AfD co-chairman Joerg Meuthen said the party was strongly positioned for next year's national elections and colleague Beatrix von Storch predicted that it would become the third largest political force in Germany in 2017.
"We're witnessing in 2017 Angela Merkel's battle for survival," she said.
Germany's changing politics - BBC's Jenny Hill in Berlin
It's being described as the "Merkel malaise".
For the second time in a month, Angela Merkel's conservatives have suffered a humiliating defeat at the regional ballot box.
Both votes are widely seen as a verdict on Mrs Merkel's refugee policy. But the result also reflects growing disillusionment with Germany's establishment parties. The Social Democrats may have won the election here but they lost voters; their success is being described as the weakest victory of all time.
Germany's political landscape is changing. The anti-migrant, anti-Muslim rhetoric of AfD resonates with the electorate. The party is now almost certain to win seats in the national parliament next year, which could complicate coalition-building. Commentators predict the start of a more complex politics.
And many blame Angela Merkel. For the first time, the chancellor's political future feels uncertain. Don't expect her to stand down any time soon. But, increasingly, her own party views her as irrevocably tainted by her refugee policy.
Bavarian Finance Minister Markus Soeder, from the CDU's sister party CSU, was quick to call it the "second massive wake-up call" in two weeks.
"A long-term and massive loss in trust among traditional voters threatens the conservative bloc," he told the Bild daily, adding Ms Merkel's right-left national coalition had to win back support by changing course on its immigration policy.
What went wrong? Media on failings of mainstream politics
"Protest election turns capital into tatters," proclaims daily tabloid Bild, which describes Berlin's SPD mayor as Germany's "weakest election winner of all time".
The rise of the right-wing AfD is driven by voters who feel "forgotten and marginalised", says Frankfurter Allgemeine Zeitung.
On the fate of the chancellor, Spiegel says that while her CDU "crashed", she is likely to be able to blame local factors, rather than anger at her welcoming stance on migrants. Sueddeutsche Zeitung sees a party revolt as unlikely: "No one knows how things will carry on without her."
A note on terminology: The BBC uses the term migrant to refer to all people on the move who have yet to complete the legal process of claiming asylum. This group includes people fleeing war-torn countries such as Syria, who are likely to be granted refugee status, as well as people who are seeking jobs and better lives, who governments are likely to rule are economic migrants.