Stockholm, Sweden (CNN) Sweden has been plunged into political uncertainty after both the main centrist coalitions failed to win a majority in general elections Sunday, and as the far-right anti-immigration party gained ground, further fracturing the vote.

A long period of political wrangling is expected as the eight parties that won seats in parliament try to build a working government. Either the center-left and center-right blocs will have to coordinate to pass legislation, or they will have to renege on promises to never work with the far-right Sweden Democrats, a party that has roots in the neo-Nazi movement and has capitalized on the nation's growing migration fears.

Leader of the far-right Sweden Democrats Jimmie Åkesson speaks at the party election center on Sunday.

The talks will be a test of one of the few remaining left-wing bastions in Europe, a region leaning further to the right after the mass migration of refugees in 2015, at the height of the Syrian war and as conflicts in Afghanistan and parts of Africa worsened.

The Sweden Democrats, who have promised to freeze migration and want to take the nation out of the European Union, gained ground with 17.6% of the vote, well up from the 12.9% it won in 2014, but short of the dramatic surge pollsters had predicted.

Its leader, Jimmie Akesson, treated the gains as an election win and basked in his role as kingmaker.