A woman walks next to election posters of (R-L) the leader of the Social Democrats and Swedish Prime Minister Stefan Loefven, Swedish Minister for Finance Magdalena Andersson and Sweden's Foreign Minister Margot Wallstrom on September 1, 2018 in Stockholm.
PHOTO: JONATHAN NACKSTRAND/AFP/AFP/Getty Images
A woman walks next to election posters of (R-L) the leader of the Social Democrats and Swedish Prime Minister Stefan Loefven, Swedish Minister for Finance Magdalena Andersson and Sweden's Foreign Minister Margot Wallstrom on September 1, 2018 in Stockholm.
Now playing
02:28
Support for far-right party surges in Sweden
A woman walks next to election posters of (R-L) the leader of the Social Democrats and Swedish Prime Minister Stefan Loefven, Swedish Minister for Finance Magdalena Andersson and Sweden's Foreign Minister Margot Wallstrom on September 1, 2018 in Stockholm.
PHOTO: JONATHAN NACKSTRAND/AFP/AFP/Getty Images
A woman walks next to election posters of (R-L) the leader of the Social Democrats and Swedish Prime Minister Stefan Loefven, Swedish Minister for Finance Magdalena Andersson and Sweden's Foreign Minister Margot Wallstrom on September 1, 2018 in Stockholm.
Now playing
02:15
Far-right party surges in Sweden
Italian President Sergio Mattarella addresses journalists after consultations with political parties, on May 7, 2018 at the Quirinale palace in Rome. - Italy is facing a third month of political paralysis after talks between the centre-left and the Five Star Movement over a coalition government failed to get off the ground. President Sergio Mattarella holds a fresh round of consultations "to see if the parties have other ideas for a government majority". (Photo by Andreas SOLARO / AFP)        (Photo credit should read ANDREAS SOLARO/AFP/Getty Images)
PHOTO: ANDREAS SOLARO/AFP/Getty Images
Italian President Sergio Mattarella addresses journalists after consultations with political parties, on May 7, 2018 at the Quirinale palace in Rome. - Italy is facing a third month of political paralysis after talks between the centre-left and the Five Star Movement over a coalition government failed to get off the ground. President Sergio Mattarella holds a fresh round of consultations "to see if the parties have other ideas for a government majority". (Photo by Andreas SOLARO / AFP) (Photo credit should read ANDREAS SOLARO/AFP/Getty Images)
Now playing
01:52
Mattarella sparks outrage with PM nomination
Now playing
03:31
In Italy's election, migration is a key issue
Now playing
00:10
This video is no longer available
Leader of the Dutch Party for Freedom (PVV) Geert Wilders looks on after the resignation of two of his MPs after the weekly debate at the Parliament in The Hague on July 3, 2012. Hernandez and Kortenoeven, two of the PVV's 24 MPs, announced their resignation from the party today, saying they disagreed with autocratic leadership style.  AFP PHOTO / ANP / ROBIN UTRECHT   ***NETHERLANDS OUT***        (Photo credit should read ROBIN UTRECHT/AFP/GettyImages)
PHOTO: ROBIN UTRECHT/AFP/GettyImages
Leader of the Dutch Party for Freedom (PVV) Geert Wilders looks on after the resignation of two of his MPs after the weekly debate at the Parliament in The Hague on July 3, 2012. Hernandez and Kortenoeven, two of the PVV's 24 MPs, announced their resignation from the party today, saying they disagreed with autocratic leadership style. AFP PHOTO / ANP / ROBIN UTRECHT ***NETHERLANDS OUT*** (Photo credit should read ROBIN UTRECHT/AFP/GettyImages)
Now playing
01:58
Meet Geert Wilders, Holland's Donald Trump
why dutch fishing village support nationalist candidate atika shubert_00024028.jpg
why dutch fishing village support nationalist candidate atika shubert_00024028.jpg
Now playing
03:22
Dutch fishermen support nationalist candidate
Netherlands elections Shubert lkl_00013003.jpg
Netherlands elections Shubert lkl_00013003.jpg
Now playing
02:02
Dutch reveal who they are voting for
netherlands turkey relations shubert pkg_00015126.jpg
PHOTO: EENVANDAAG
netherlands turkey relations shubert pkg_00015126.jpg
Now playing
02:30
Netherlands set for parliamentary vote
Scottish First Minister Nicola Sturgeon says beginning Brexit would deprave people and businesses in Scotland of rights and freedoms.
PHOTO: Jeff J Mitchell/Getty Images
Scottish First Minister Nicola Sturgeon says beginning Brexit would deprave people and businesses in Scotland of rights and freedoms.
Now playing
02:30
Scotland calls for independence referendum
french rural town turns to national front party melissa bell_00000000.jpg
french rural town turns to national front party melissa bell_00000000.jpg
Now playing
02:41
French rural voters hope Le Pen brings change
Leader of the french far-right Front National (FN) party Marine Le Pen holds a press conference on March 9, 2015 in Metz, eastern France, ahead of the March 22 and 29, 2015 regional elections. AFP PHOTO / JEAN-CHRISTOPHE VERHAEGEN        (Photo credit should read JEAN-CHRISTOPHE VERHAEGEN/AFP/Getty Images)
PHOTO: JEAN-CHRISTOPHE VERHAEGEN/AFP/Getty Images
Leader of the french far-right Front National (FN) party Marine Le Pen holds a press conference on March 9, 2015 in Metz, eastern France, ahead of the March 22 and 29, 2015 regional elections. AFP PHOTO / JEAN-CHRISTOPHE VERHAEGEN (Photo credit should read JEAN-CHRISTOPHE VERHAEGEN/AFP/Getty Images)
Now playing
01:47
Who is Marine Le Pen?
european shift nationalism elections nic robertson lead live_00033423.jpg
PHOTO: AFP
european shift nationalism elections nic robertson lead live_00033423.jpg
Now playing
03:53
Nationalist wave spreads throughout world
ROME, ITALY - DECEMBER 05:  Italian Prime Minister Matteo Renzi give a speech after the results of the referendum on constitutional reforms at Palazzo Chigi on December 5, 2016 in Rome, Italy. The result of the government referendum that could change the constitution is considered crucial for the political future of Italy and for the personal future of its Prime Minister.  (Photo by Franco Origlia/Getty Images)
PHOTO: Franco Origlia/Getty Images Europe/Getty Images
ROME, ITALY - DECEMBER 05: Italian Prime Minister Matteo Renzi give a speech after the results of the referendum on constitutional reforms at Palazzo Chigi on December 5, 2016 in Rome, Italy. The result of the government referendum that could change the constitution is considered crucial for the political future of Italy and for the personal future of its Prime Minister. (Photo by Franco Origlia/Getty Images)
Now playing
01:01
Italians vote 'no,' a win for Eurosceptics
Now playing
06:04
Former Italy PM: Media misunderstood referendum
austria presidential race shubert pkg_00010712.jpg
austria presidential race shubert pkg_00010712.jpg
Now playing
03:21
Far-right rejected in Austria's election
(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.
PHOTO: Michael Campanella/Getty Images
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.

“We see that we are this election’s winner, but now we enter a new mandate period and now we are going to get influence over Swedish politics for real,” Akesson told supporters in a speech as the results rolled in.

He called on other parties to enter into talks with the Sweden Democrats. He singled out the centre-right’s candidate for premier, Moderates leader Ulf Kristersson, for possible negotiations.

Swedish Prime Minister Stefan Löfven of the Social Democrats, which leads the center-left bloc, called for cooperation across the political divide and warned against inviting the far right into government.

Voters mark their choices at a polling station in Stockholm on Sunday.
PHOTO: JONATHAN NACKSTRAND/AFP/Getty Images
Voters mark their choices at a polling station in Stockholm on Sunday.

He reminded Swedes that he would remain in office in the coming weeks and asked people not to speculate on the election’s official outcome.

“We have two weeks left until parliament opens. I will work on calmly, as prime minister, respecting voters and the Swedish electoral system,” Löfven told a party rally.

Writing on Twitter in the wake of the preliminary results, former Swedish prime minister Carl Bildt of the center-right Moderates advised Löfven to step down and allow the speaker of parliament to begin preliminary consultations on a new government.

“This would reduce uncertainty and mitigate friction between all parties,” Bildt said.

If the ruling coalition fails to form a government it would be a historic loss for the Social Democrats, which have dominated Swedish politics since the 1930s.

The Social Democrats came in first as expected, but won just 28.4% of the votes, its worst showing in more than a century. Its coalition won 40.6% of the vote, just a hair’s breadth ahead of the opposition center-right bloc, which gained 40.3%.

Those results will be finalized on Wednesday and could change before election authorities make them official.

Political analyst Nicholas Aylott from Södertörn University told The Local Sweden that a German-style “grand coalition” between the main Social Democrats and Moderates was possible, but another alternative might be an alliance under a new prime minister.

“It might not be what Sweden really needs. But it is not a recipe for chaos.”

Migration a game-changer

Although the Sweden Democrats did not fulfill predictions in exit polls that it would become the nation’s second-biggest party, its success has confirmed fears that Europe’s rising far-right tide has now reached Sweden, one of the world’s most liberal countries.

Immigration became highly politicized following a steady increase in new arrivals. In 2015, Sweden, a country of 10.1 million people, took in more than 160,000 asylum-seekers.

Support for the far right in Sweden mirrors similar trends in other European nations. Anti-migrant parties in Germany, Austria, Denmark, France, Hungary, Italy and the UK have all made gains in recent years.

Senior Social Democrats official Anders Ygeman conceded that the crisis had damaged his coalition’s prospects.

“We paid the price for being in government. We suffered from the refugee crisis in 2015. That’s why the Sweden Democrats are as big as they seem to be,” he told CNN in Stockholm.

The results show Sunday’s vote was one of the toughest challenges in decades to Sweden’s social democracy, characterized by high tax rates and a substantial welfare system aimed at reducing inequality through social inclusion.

Leaders in Brussels will be disappointed with the far right’s gains ahead of the European Parliament’s elections in May next year, as they bid to discourage euroskepticism following the UK’s decision to leave the EU, and as populist parties form alliances to shake up the EU establishment ahead of the vote.

Far-right populists in Europe, however, were celebrating the Sweden Democrats’ rise.

Marine Le Pen, leader of France’s far-right euroskeptic National Rally, said on Twitter: “Another bad night for the European Union in perspective. The democratic revolution in Europe is underway!”

Italy’s euroskeptic Deputy Prime Minister Matteo Salvini tweeted that Sweden had decided to change after years of “savage migration.”

“Sweden, land of the multiculturalism and model of the left, after years of savage immigration has finally decided to change. Now, together with Jimmie Akesson, I say no to this Europe of bureaucrats and speculators, no to the clandestine [immigrants], no to Islamic extremism.”

CNN’s Atika Shubert and journalist Linnéa Wannefors reported from Stockholm. CNN’s Angela Dewan wrote from London and Samantha Beech contributed from Atlanta.