"So tonight I hand over the keys to the prime minister's office to Helle Thorning-Schmidt. And dear Helle, take good care of them. You're only borrowing them," Loekke Rasmussen said.

The result means Denmark will get a new government that could roll back some of the austerity measures introduced by Loekke Rasmussen amid Europe's debt crisis.

A majority for the "red bloc" also deprives the anti-immigration Danish People's Party of the kingmaker role it has used to tighten Denmark's borders and stem the flow of asylum-seekers.

The opposition won 89 of the mainland seats compared to 86 for the governing coalition, according to preliminary results with 99 percent of votes counted. The "red bloc" was expected to win at least two of the four seats allocated to the semiautonomous territories of Greenland and the Faeroe Islands.

A power shift isn't likely to yield major changes in consensus-oriented Denmark, where there is broad agreement on the need for a robust welfare system financed by high taxes.

But the two sides differ on the depth of austerity measures needed to keep Denmark's finances intact amid the uncertainty of the global economy.

Thorning-Schmidt, 44, wants to protect the welfare system by raising taxes on the rich and extending the average working day by 12 minutes.

Loekke Rasmussen says tax hikes would harm the competitiveness of a nation that already has the highest tax pressure in the world.

"We need sound public finances without raising taxes," Loekke Rasmussen, 47, told reporters after casting his ballot in Graested, north of Copenhagen.

The Social Democrats teamed up with the working-class Socialist People's Party and also count on support from the centrist Social Liberals and the small, left-wing Red-Green Alliance,

Loekke Rasmussen took credit for steering Denmark through the financial crisis in better shape than many other European countries. However, the rebound has been slower than in neighboring Nordic nations and the government projects budget deficits of 3.8 percent of gross domestic product in 2011 and 4.6 percent in 2012.

Although Denmark isn't part of the debt-ridden eurozone, its currency is pegged to the euro and the country's export-driven economy is affected by shocks from Europe and beyond.

The government's reforms include gradually raising the retirement age by two years to 67 by 2020 and trimming benefit periods for early retirement and unemployment.

The economy emerged as the top election theme, to the chagrin of the Danish People's Party, which has used its kingmaker role in previous elections to push through immigration laws that are among Europe's toughest.

Thorning-Schmidt isn't likely to make any major changes to those laws, but she's promised to overhaul a system of beefed-up customs controls at borders with Germany and Sweden, which critics say violates the spirit of EU agreements on the free movement of people and goods.