Brussels • 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 still 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 they placed third, capturing 18 percent of initial tallies of the vote. 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 seize power with support from the Sweden Democrats.

"Now we will gain influence in Swedish politics for real," Sweden Democrat 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.

The result was 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 migration. Sweden, a nation of 10.2 million, took in 163,000 asylum seekers in 2015, the highest 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.

Arrivals dropped the following year 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 as overall crime figures remained flat or even improved, according to criminologists. Just last month, a spate of more than 80 arsons in a few hours in the Swedish city of Gothenburg drove Social Democratic Prime Minister Stefan Löfven 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 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 Löfven resign based on the 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 previous election in 2014.

Sweden’s struggles have captured attention around the world, including in the United States, where President Donald 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, notably Italy, where a government coalition that includes the far-right League party came to power this year and began an ambitious campaign to discourage migration.