In an election that turned on economic uncertainty and fierce debates over immigration, Danish voters on Thursday ousted their center-left government in a clear swing to the right that unexpectedly elevated an anti-immigrant, anti-European Union party that had been on the margins of the country’s politics.

Polls had predicted a close race, but as the night wore on, the far-right Danish People’s Party emerged in second place over all, raising questions about the role it could play in a new government and the country’s path in the coming four years.

The outcome took even senior members of the Danish People’s Party by surprise. “It’s gone beyond my wildest expectations,” Peter Skaarup, a senior lawmaker with the party told The Local, a Danish news outlet. “I know we often fare better in these elections than the polls suggest since people often aren’t willing to admit that they vote for the Danish People’s Party, but it really does look fantastic so far.”

Based on preliminary results published by national broadcaster DR.DK, the center-right bloc that includes the Danish People’s Party secured a majority of 90 seats in Parliament. That would allow it to form the next government, with the leader of the conservative Liberal Party, Lars Lokke Rasmussen, expected to become prime minister.

Although the Danish People’s Party won more votes than the Liberals, none of Denmark’s many smaller parties was willing to form a government with it, according to Kasper M. Hansen, a professor of political science at the University of Copenhagen. The election, called by Prime Minister Helle Thorning-Schmidt just three weeks ago, came at a time when Danes increasingly feared that their cherished system of generous welfare benefits was being abused by migrants from southern Europe and a recent surge of more than 14,000 asylum seekers, mostly Muslims.

The country remains shaken by a Feb. 14 shooting rampage in Copenhagen by the 22-year-old son of Palestinian immigrants at a free-speech event and outside a synagogue that left two people dead and five police officers wounded.

With all votes from the mainland counted, DR.DK showed Ms. Thorning-Schmidt’s Social Democrats emerging as the strongest party, with 26.3 percent of the vote, yet without enough seats to form a government.

That task appeared to go to Mr. Rasmussen, 51, despite a weak showing for his Liberals.

The Danish People’s Party’s won 21.1 percent of the vote, according to DR.DK. That compares with only 12.3 percent four years ago.

Polls had consistently shown the two main political alliances neck and neck. Denmark has a single-chamber Parliament with 179 members, of which 175 are elected in Denmark itself, and two each by the country’s two former colonies, the Faroe Islands and Greenland.

Ms. Thorning-Schmidt, who became the country’s first female leader in 2011, tumbled in popularity after unpopular economic changes passed by her government, while the economy dipped in and out of recession. Yet signs of an economic revival in recent weeks helped bolster her alliance in polls, although her main challenger, Mr. Rasmussen, claimed the improvement was linked to changes introduced by his government when he led the country from 2009 to 2011.

“We won the campaign, but not the election,” Ms. Thorning-Schmidt, 48, told her party, announcing she was quitting as leader of the Social Democrats after the announcement of the outcome early Friday.

Both of the leading parties had pledged a tougher stance on immigration, with the prime minister campaigning on a vow to require refugees to work — an unusual position for her party.

Denmark has consistently ranked among the world’s happiest nations, but the flow of immigrants ignited a backlash that has heightened nationalist sentiments, something that also unfolded with political upheaval in neighboring Finland — where the populist Finns Party joined the government — and to some extent in other European countries.

“Immigration has been a very key and decisive issue in this campaign,” Mr. Hansen said. Debate focused largely on the number of workers coming from places like Bulgaria and Romania, what sort of benefits they should receive, and whether Denmark should take in more of the migrants arriving at Europe’s southern borders, he added.