Italy’s anti-establishment Five Star Movement (M5S) and the centre-left Democratic party (PD) have agreed to try to form a new government in a move that could avert snap elections and push the far-right League into opposition.

Italy plunged into chaos this month after Matteo Salvini withdrew his League party from its fractious alliance with M5S as he sought to exploit the party’s popularity to bring about snap elections and become prime minister.

The dramatic move threatened to create a fully far-right government.

But Salvini, whose tactics have dented his popularity in recent weeks, had not banked on M5S teaming up with the PD. The pair are longstanding enemies but also the two largest parties in parliament.

Salvini also had not expected Giuseppe Conte, the outgoing prime minister who ended the ill-fated M5S-League alliance last week, to emerge as his rival. Talks between the PD and M5S only progressed after Nicola Zingaretti, the PD leader, succumbed to demands from his M5S counterpart, Luigi Di Maio, to reinstate Conte.

“There is a political agreement with the PD allowing Conte to receive the mandate to form a long-term government,” Di Maio said after meeting the president, Sergio Mattarella, on Wednesday evening.

Di Maio also said that Salvini had tried to patch things up and restore their government, offering him the role of prime minister in return.

“I refused,” he added. “I’m interested in what is best for the country, not what is best for me. I don’t deny the work done over the past 14 months, and the recognition of Conte by [Donald] Trump is a sign that we are on the right path.”

The US president endorsed Conte in a tweet, saying he was “a highly respected prime minister”.

Mattarella met leaders of Italy’s main parties on Wednesday to explore the possibility of an alternative majority. He could announce that he is charging Conte with the task by Thursday. Mattarella said last week that unless the parties could come up with a convincing pact, he would call snap elections.

An embittered Salvini made a final push for snap elections as he lambasted the potential tie-up between his former ally and the PD.

“Mattarella has asked for a government with long-term prospects … can anyone seriously tell me that a government between M5S and the Democratic party is a long-term prospect? It will be a long agony,” he said after meeting the president.

Salvini said that those who fear elections might escape a ballot for “three or six months” but in the end they would have to face a League that was ready to give Italy a “strong and coherent” government.

“Whichever way the popular judgment goes, we will face it head-on while some will have to equip themselves with masks.”

Zingaretti said after his meeting with Mattarella that his group was ready to try to form a government with M5S, but that it needed to be a “turning point” administration that broke from the past.

Discussions between the two parties have been fraught, partly due to their deep-seated rivalry but also because of clashes over Conte as well as who should lead the various ministries.

The two sides still need to iron out a programme and decide on a cabinet. There is a risk that the deal could be scuppered after M5S said it would ask members to vote on any agreement with the PD on Rousseau, the online network it often uses to make policy decisions.

It is unclear how the votes are counted but many M5S activists are against the alliance. M5S said the vote could be held over the next week. If Conte is given a mandate, then he would probably be given until early next week to formalise a plan.

Mattarella has summoned Conte to a meeting on Thursday morning.