People vote in polling booths Sunday in Stockholm. (Hanna Franzen/AP)

Swedish voters angry about crime and migration on Sunday delivered a blow to two centrist parties that have traded power for decades, but an insurgent far-right party fell short of capturing a commanding position inside the parliament.

The election had been watched closely for signs about the extent to which a cascade of anti-immigrant fear could hit even Sweden, which has long been one of Europe’s most open nations toward refugees.

The far-right Sweden Democrats had at times during the campaign appeared to have a shot at becoming the biggest party in the country, but in the end the party placed third, capturing 18 percent of the vote, according to initial tallies. Still, they succeeded in defining the election’s agenda and expanded their power in parliament.

Now Swedish leaders will head into a chaotic period of politicking as they seek to build a ruling coalition out of the fragments of their old political landscape. Both the ruling center-left Social Democrats and the center-right Moderates had among their worst results in modern Swedish political history.

The coalition blocs that each party leads were neck-and-neck with each other, leaving the ultimate result in doubt and raising the possibility the center-right group might seek to rule with support from the Sweden Democrats. 

Jimmie Akesson, leader of the far-right Sweden Democrats, speaks to supporters at the party election center Sunday. (Michael Campanella/Getty Images)

“Now we will gain influence in Swedish politics for real,” the Sweden Democrats’ leader, Jimmie Akesson, told a cheering crowd of supporters as the results came in. He said his party had “won” the elections because of its gain in seats.

The Sweden Democrats want to slam the door to new arrivals, pull out of the European Union and significantly increase the rate of deportations. In the past, Akesson has condemned the spread of mosques and Muslims.

“All they can offer are growing divisions and hatred,” said Swedish Prime Minister Stefan Lofven, who defied calls to step down after leading the Social Democrats to their worst showing since 1908, although they remained the largest party at 28 percent.

The election results were a mark of the success Akesson has had in gentrifying his party, which traded in its black boots and swastikas for suits and has sought to portray itself as a defender of ordinary working Swedes. Although both major parties have ruled out formally ruling with the Sweden Democrats in a coalition, the center-right Moderates have said they would not reject support in areas where the parties’ positions coincide. 

One of those areas is likely to be immigration. Sweden, a nation of 10.2 million, took in 163,000 asylum seekers in 2015, the highest rate per capita in Europe. Although the country initially welcomed the new arrivals, moods quickly soured amid fears that the wave of people fleeing war and poverty could capsize Sweden’s generous social welfare system. Leaders soon imposed border controls and started talking about large-scale deportations.

Supporters react at the Social Democratic Party's election party in Stockholm on Sunday. (Claudio Bresciani/AFP/Getty Images)

Arrivals dropped in 2016 to levels seen in previous years. But the surge crystallized long-running worries about Sweden’s ability to integrate immigrant groups, turning what had been a taboo issue into one that dominated airwaves and the political conversation.

A string of high-profile crimes, including arsons, stoked the discussion even though overall crime figures remained flat or even improved, according to criminologists. Last month, a spate of more than 80 arsons in a few hours in the city of Gothenburg drove Lofven to toy with deploying the military to heavily immigrant neighborhoods outside city centers.

The center-right Moderates have become especially tough on immigration, echoing many of the positions of the Sweden Democrats. That raised questions about whether the parties might find a way for the far-right party to vote with the Moderates at least part of the time and helping to install a center-right leader in the prime minister’s office. Moderates leader Ulf Kristersson hinted Sunday that could be his goal by demanding that Lofven resign based on the election results.

The election also touched on the future of Sweden’s generous welfare state, as voters searched for the best way to secure it in coming decades. The Sweden Democrats’ strong showing was echoed on the far left by a pop for the ex-communist Left Party, which captured 7.9 percent of the vote, up 2.2 percentage points from the election in 2014.

Sweden’s struggles have captured attention around the world, including in the United States, where President Trump has at times held it up as an example of the failures that come from too much immigration. If Sweden takes a more restrictive approach to its borders, it would join other European countries in tightening migration rules. In Italy, a government coalition that includes the far-right League party came to power this year and began an ambitious campaign to discourage migration.