Denmark's Liberal Prime Minister Lars Lokke Rasmussen has conceded victory in the country's general election, paving the way for Social Democrat leader Mette Frederiksen to take power.
At 41, Ms Frederiksen is set to be the country's youngest ever prime minister.
The centre-left Social Democrats won 25.9% of the vote and her bloc secured 91 of the 179 seats in parliament.
Support for the anti-immigration Danish People's Party (DPP) dived, four years after becoming second biggest party.
Immigration was still a key factor in the election, along with climate change and welfare cuts, and the Social Democrats bolstered their support with a tougher migration policy.
"Together we have created a hope that we can change Denmark, that we can improve Denmark," Mette Frederiksen told supporters.
Denmark becomes the third Nordic country in a year to elect a leftist government, following Sweden and Finland.
Mr Rasmussen's Liberal Party has been in power for 14 of the last 18 years and won power from the Social Democrats in 2015.
"We had a really good election, but there will be a change of government," the prime minister conceded.
Why did centre left win?
During the campaign, the Social Democrats pledged to increase public spending, increase taxes for businesses and the wealthy, and partially roll back pension reforms so that people who have worked for 40 years can retire earlier.
Denmark's main parties are traditionally organised into the left-wing "red bloc" and right-wing "blue bloc".
The five-party red bloc is headed by the centre-left Social Democrats, who have spent the past four years in opposition.
The blue bloc is splintered into eight parties.
The pro-EU Liberal Party (Venstre) has ruled Denmark since 2015, forming a coalition with the Conservative People's Party and the Liberal Alliance.
Venstre improved on its 2015 election performance, increasing its share of the vote by almost 4% to 23.4%. But its Liberal Alliance was one of the biggest losers, seeing its seats fall from 13 to four in the Folketing (parliament). Its leader Anders Samuelsen was under pressure to resign in the wake of the results.
Venstre also became the biggest Danish party in the European Parliament last month and promised in the national elections to put extra money into the welfare system and crack down on immigration.
The nationalist DPP, which supported the Venstre-led government lost 21 of its 37 seats, the biggest such fall since 1973.
What were the key issues?
Welfare: Many voters were concerned over the fate of the country's cherished welfare model, which has faced years of cuts amid struggles by successive governments to deal with ageing populations. Danes pay some of the highest taxes in the world to support a generous cradle-to-grave welfare state. But they say cuts mean they are increasingly paying for services that used to be free and worry that further austerity measures could erode key services such as health care and education.
The environment: Some 57% of Danes wanted the next government to prioritise climate change, according to a Gallup poll released in February. For younger voters, aged between 18 and 35, the figure was 69%.
Immigration: Major parties on the right and left adopted anti-immigration policies. Both the Liberal Party and the Social Democrats said doing so would help to protect the welfare system.