The results, based on broadcasters’ tallies of more than 90 percent of the vote, set the country on course to having its first female prime minister, Helle Thorning-Schmidt, whose party led an alliance known as the red bloc. She claimed victory about three hours after the polls closed. “We’ve written history today,” Ms. Thorning-Schmidt said.

For Denmark, a nation of 5.5 million people, the election turned on the issue that has also divided many other Western nations struggling with low growth, large government deficits and historic levels of national debt: what mix of government spending and tax policies to adopt in order to restore economic health and avoid slipping further toward a crisis like Greece’s.

The Social Democrats’ campaign was based on Ms. Thorning-Schmidt’s promises to raise taxes on Denmark’s banks and its wealthiest citizens to pay for better schools and hospitals, and to finance a $4 billion expansion of what is already one of Europe’s most generous welfare systems. Her most headline-catching nod to austerity came with her proposal to add 12 minutes to the average Dane’s working day, and she also acknowledged the need to take action on the deficit, which is set to rise to 4.6 percent of gross domestic product in 2012, well above the European average.

The departing center-right bloc, led by Mr. Loekke Rasmussen, pointed to Europe’s deepening financial crisis and said Denmark should keep tight control of its public finances, avoiding “uncontrolled debt,” and not adopt tax increases that would stifle its weak economic recovery, with a projected growth rate this year of 1.25 percent. Denmark is Scandinavia’s worst-performing economy, with a growth rate less than half of Norway’s and less than one-third of Sweden’s.

Ms. Thorning-Schmidt built an early lead in the polls, which narrowed sharply in the last days of the campaign, and on election night her coalition appeared to have a sliver of a parliamentary majority. At midnight in Copenhagen, the election count indicated that the red bloc had 89 seats in the 179-seat Parliament and was likely to secure another 3, with Mr. Loekke Rasmussen’s blue bloc winning 86 seats and likely to win one more.

An error has occurred. Please try again later.

You are already subscribed to this email.

One feature of the decade-long rule of the center-right parties that seemed unlikely to change significantly was the tight new restraints on immigration, which have most heavily affected Denmark’s population of about 200,000 Muslims, many of them asylum seekers. With Muslims now accounting for nearly 4 percent of the population, according to the United States State Department, and Islam now the largest non-Christian denomination in the country, opinion polls have shown strong support for the controls, which Ms. Thorning-Schmidt said any government she led would maintain.

Although they regained power, the Social Democrats scored slightly less well than they did in the previous election, in 2007, winning 44 seats, one down from their 2007 tally.

The anti-immigration Danish People’s Party, which formed the third-largest bloc in the previous Parliament, won 22 seats, down 3 seats from 2007. It will become an opposition party, and Pia Kjaersgaard, the party’s leader, said it would oppose any relaxation of immigration controls. The party pushed for Denmark’s withdrawal from the so-called Schengen group of countries that established a ’’borderless’’ Europe, but the country did not leave the group.

Correction: October 22, 2011

An article on Sept. 16 about a national election in Denmark that the ruling center-right party lost to a center-left coalition misstated the outcome of a push by the Danish People’s Party, an anti-immigration party, for Denmark’s withdrawal from the so-called Schengen agreement, which creates what has been called a “borderless” Europe. The Danish People’s Party, which had often voted with the ruling party, failed in its efforts to persuade the country’s leaders to pull out of the agreement. The party did not successfully push for Denmark’s withdrawal. Readers pointed out the error in e-mails to The Times on Sept. 16. This correction was delayed for research on other points in the article.

A version of this article appears in print on September 16, 2011, on Page A7 of the New York edition with the headline: Voters Put Leftists in Power in Denmark. Order Reprints|Today's Paper|Subscribe