Denmark's centre-right opposition has won the parliamentary election after strong gains by an anti-immigration party that wants to limit the European Union's influence over the country.
With all votes counted, preliminary official results showed the opposition bloc led by Lars Loekke Rasmussen, a former prime minister, would obtain the 90 seats needed to secure a majority in the 179-seat parliament.
"Four years ago we handed over the keys to the prime minister's office. I then said it was only a loan," he told supporters in Copenhagen. "There is a majority that believes that Denmark needs a new government and gives us a possibility to get the keys back."
His main opponent, p rime minister Helle Thorning-Schmidt, the daughter-in-law of former Labour leader Neil Kinnock and Denmark's first woman leader, conceded defeat, saying she would resign as premier and leader of the Social Democratic Party.
"We were beaten on the finish line," she said. "I know that Lars Loekke loves Denmark and he should be happy that he takes over a Denmark that is in great shape."
Mr Loekke Rasmussen's Liberal Party lost support but the centre-right bloc still obtained a majority due to gains by the Danish People's Party, a Eurosceptic populist group opposed to immigration.
The Danish People's Party surged to 21%, helping the opposition get 51.5% of the vote, results showed.
It was not immediately clear whether the party would seek cabinet posts or try to use its leverage in parliament to influence the government, as it did before 2011.
"The most important for the Danish People's Party is to get political influence," leader Kristian Thulesen Dahl said.
Mr Loekke Rasmussen was expected to become prime minister because he has the support of the other members of the bloc.
Before the vote, opposition parties endorsed British prime minister David Cameron's bid for EU reforms, which suggests that Denmark, may also consider a looser relationship with the union.
The Danish People's Party has called for Denmark to take back more authority from the EU headquarters in Brussels and for border controls to be reintroduced on the boundaries with Germany and Sweden. That is controversial among many EU members who feel it would challenge the spirit of a borderless Europe.
Election campaigns focused on welfare spending, the economy and immigration, with both Ms Thorning-Schmidt and Mr Loekke Rasmussen promising to further tighten Denmark's controls on immigration.
"I want an open Denmark, but I also want a Denmark that is efficiently shut for people who don't want our country," Mr Loekke Rasmussen said when he voted.
Ms Thorning-Schmidt, who is married to Labour MP Stephen Kinnock, lost the election even though her Social Democratic Party improved slightly and remained Denmark's largest party. The smaller members in her coalition lost support.
An exit poll by broadcaster TV2 had projected a smaller margin that would have given the semi-autonomous Faroe Islands and Greenland the decisive votes. The North Atlantic territories have two seats each in the legislature.
But even though all four seats ended up going to Ms Thorning-Schmidt's coalition, it was not enough. Her bloc won 89 seats in total, one short of a majority.