Sweden’s parliament has voted to give the Social Democrat leader, Stefan Löfven, a second term in office at the head of a new centre-left minority government, ending more than four months of deadlock following an inconclusive election.

The caretaker prime minister will take office on Monday, governing in a coalition with the Green party and with the parliamentary backing of the Centre and Liberal parties, formerly members of the four-party centre-right opposition Alliance.

The 9 September election produced a hung parliament, with the centre-right and centre-left blocs that have dominated Swedish politics for decades each securing about 40% of the vote and separated by a single seat, heralding months of complex coalition talks.

Neither bloc was easily able to form a new government without in some way involving the third-biggest party, the far-right, anti-immigration Sweden Democrats, with whom all other parties have refused to cooperate at national level.

“More and more governments are becoming reliant on parties with an anti-democratic agenda,” Löfven said after winning the vote in parliament. “But in Sweden we stand up for democracy, for equality. Sweden has chosen a different path.”

Both he and the centre-right leader, Ulf Kristersson, had tried unsuccessfully since the vote to forge potential ruling alliances in parliament. Only two more attempts were allowed before a snap election was due on 23 January.

Löfven’s second attempt was approved because although only 115 MPs voted in favour and 153 against, with 77 abstentions, there was no majority against him. Under Swedish law, prime ministers can govern as long as a majority in parliamentary do not actively vote against them.

The former welder and trade union leader succeeded in peeling the Centre and Liberal parties away from their centre-right bloc by offering major concessions, promising notably to cut taxes, reform the rental housing market and relax Sweden’s strict employment laws.

While that allowed him to finally form a government, it will be one of Sweden’s weakest in living memory. Just 33% of voters cast their ballots for the two coalition parties, and with the Centre and Liberal parties the new administration holds 167 seats, eight short of a majority in the 349-seat Riksdag.

If the former communists of the Left party, who like the Centre and Liberal parties abstained in Thursday’s vote, decide the government is swinging too far to the right and vote against it, the opposition – the Sweden Democrats plus the two remaining centre-right parties, the Moderates and Christian Democrats – would have a majority.

“The constellation that has chosen this prime minister is only united by one thing: a democratically dubious desire to exclude other parties from influence – not to let their votes count,” said Kristersson, the Moderate party leader

The Sweden Democrats leader, Jimmie Åkesson, who had hoped for more from the elections after his party won a record 17.5% of the vote, described the outcome of the talks as absurd. “My ambition now is that the Sweden Democrats will be a dominating force in a new strong centre-right opposition,” he said.