The leaders of Sweden’s main left and rightwing parliamentary blocs have staked claims to form the next government, heralding weeks or months of fraught negotiations after an election that left them almost tied and far short of a majority amid gains by both the far right and smaller parties.

The incumbent prime minister, Stefan Löfven, whose Social Democrats remained the largest party with 28.4% of the vote, its lowest score for a century, refused opposition calls to resign and said he would take two weeks before the next parliamentary vote to try to build a cross-bloc coalition that would avoid the growing influence of the populist, anti-immigrant Sweden Democrats.

The election marked “the end of bloc politics in Sweden”, Löfven told supporters. “There is no side with a majority. So it is only natural to work across the political divide to make it possible to govern.” He said a party “with Nazi roots” had nothing to offer the country but division and hatred. “We have a moral responsibility.”

Löfven’s three-party centre-left bloc looks on course to win 144 of the 349 seats in the Riksdag, just one seat more than the centre-right – although that could yet change with 200,000 overseas votes still to be counted.

The four-party centre-right alliance of the Moderate, Christian Democrat, Liberal and Centre parties called on the prime minister to step down. “This government has had its chance. It has to resign,” said the Moderate party leader, Ulf Kristersson. He said he wanted to build a government that would “unite our country and take responsibility”.

Long shunned by the other parties for their white supremacist roots, the Sweden Democrats, who framed the election as a straight choice between ending immigration and preserving Sweden’s welfare state, finished third behind the Moderates with 17.6% of the vote, a solid advance on the 12.9% they won in 2014 but far from the 25-30% they had hoped for and polls had predicted.

“We have strengthened our role as kingmakers. We are going to gain real influence over Swedish politics,” declared the Sweden Democrat leader, Jimmie Åkesson, who has said he wants to cooperate with other parties, particularly the Moderates, but will demand curbs on immigration in exchange for his party’s backing.

Other party officials echoed his call. “When the same party time and again increases, and the other parties stand still, then you have to listen to that part of the population that is voting for this party,” said the Sweden Democrats’ parliamentary leader, Mattias Karlsson. “It’s time to talk.”

Kristersson rebuffed the far-right party’s overtures, telling supporters: “We have been completely clear during the whole election. The alliance will not govern or discuss how to form a government with the Sweden Democrats.”

If Kristersson sticks to his word, which many observers doubt, the other options open to both the centre-right and centre-left look no easier. “However the dramatic bloc battle plays out, it looks like it will be difficult for Sweden to have a functioning government,” the Dagens Nyheter daily said in its editorial.

Svenska Dagbladet said “a lot of imagination” would be needed, while the popular tabloid Expressen summed up the situation in one word: “Chaos.”

On the left, which has consistently and forcefully rejected any form of cooperation with the Sweden Democrats, any attempt by Löfven to build the same administration he formed last time – a coalition with the Greens, with parliamentary backing from the Left party – would risk falling to Sweden Democrat opposition at the first hurdle.

He will therefore try to enlist the backing of two smaller parties on the centre-right, the Centre and Liberal parties. This will not be straightforward, not least because they disagree strongly with the ex-communist Left, particularly on Europe and economic policy.

“If the red-green bloc is bigger, the Centre and the Liberals hold the key and not Jimmie Åkesson,” Mikael Gilliam, a political scientist at Gothenburg University, told Swedish public radio.

Sweden: far right gains threaten Europe's most stable political order Read more

The situation on the right is no less complicated. The Moderate-led alliance that ruled in coalition from 2006 to 2014 would need the informal support of the Sweden Democrats to be assured of a parliamentary majority – something the Centre and Liberal parties are vehemently opposed to, although some Moderates and Christian Democrats could accept.

Despite decades of animosity between the two, the Moderates and Social Democrats may explore some form of broad-based cross-bloc cooperation. Analysts suggest this might not be entirely impossible: over the four-year term of the outgoing government, the two parties agreed 26 pieces of legislation, notably on immigration, energy and the climate.

“Expect lengthy and messy negotiations, as well as a sharpening of rhetoric: there is a risk that a government is not formed by the end of 2018,” said Ana Luís Andrade, an analyst at the Economist Intelligence Unit. She said she saw a minority Moderate-led government with ad hoc Sweden Democrat support as the most likely outcome.