Spanish Prime Minister Mariano Rajoy | Pablo Blazquez Dominguez/Getty Images The end of Mariano Rajoy Socialist Pedro Sánchez secures support for motion of no confidence, and will almost certainly become the new prime minister.

MADRID — Mariano Rajoy’s time as Spain’s prime minister is almost certainly over after the opposition Socialists secured parliamentary backing to oust the conservative leader.

A majority of Spanish lawmakers on Thursday said they would back Socialist leader Pedro Sánchez’s motion of no confidence in the PM in a vote on Friday. If they are as good as their word, Rajoy will be out — and become the first leader of the country to be ousted in such a way — and Sánchez would immediately replace him as prime minister.

Sánchez was promised the backing of the far-left Podemos, two Catalan pro-independence parties and the Basque Nationalist Party (PNV), although the latter tested Rajoy's nerves by waiting until late Thursday to make their decision.

If the vote goes as planned, only Rajoy's resignation could stop Sánchez from becoming PM, at least in the short term. The Socialist chief invited Rajoy to resign during a speech in parliament Thursday, promising to withdraw the no-confidence motion if he did.

“Quit and this will be all over,” Sánchez told his rival.

Sánchez looks set to become the first Spanish PM to be appointed without having won an election.

If the conservative leader does resign on Thursday night or early Friday, something his aides have ruled out, the conservatives have said that it would only delay Sánchez’s coronation while a parliamentary vote was held to elect a new prime minister.

Sánchez looks set to become the first Spanish PM to be appointed without having won an election. He would lead a fragile minority government that would have to deal with fierce opposition from Rajoy’s wounded conservatives and the rising center-right Ciudadanos. His most immediate challenge would be to confront probably the most serious constitutional crisis to face Spain since the return of democracy in 1978, after the death of the dictator Francisco Franco: the Catalan independence challenge.

Theoretically, Sánchez could stay in power until 2020, when new national elections must be called, but he has promised to call an early ballot at an unspecified date.

“Call elections if you dare and show you’re not afraid of the Spanish people,” Ciudadanos leader Albert Rivera challenged the PSOE leader in Thursday’s marathon debate, no doubt boosted by opinion polls that put his party in the lead.

Sánchez told POLITICO in an interview two weeks ago, before the confidence vote strategy emerged, that it was his intention to become prime minister at the head of a minority Socialist government.

“It’s my aspiration to govern alone, with support [from other parties] on specific policies, which is what I think the country needs,” he said.

Corruption cases

The Socialists launched the initiative to oust Rajoy following a court ruling earlier this month in which 29 people, including former senior officials from the prime minister's conservative Popular Party (PP), were sentenced to a total of 351 years in prison for corruption.

Sánchez presented the motion of censure in Congress by accusing the conservative leader of asking Spaniards to accept corruption “as a chronic illness that you want to heal by looking to the other side.” He defended the “unquestionable moral strength” of his own initiative.

Rajoy counterattacked by listing the many graft cases involving Sánchez’s PSOE over the years. “Are you [Mother] Teresa of Calcutta?” the conservative leader asked at one point. “There is corruption everywhere, as you well know because you’re near it.”

Rajoy also hit back by arguing that the only reason behind Sánchez’s bid for power is “his little confidence in reaching the premiership the way everyone else does — that is, by winning at the ballot box.”

The Socialists came second behind the PP in the 2016 general election, though both now face a serious challenge from the fast-growing Ciudadanos, which wants new elections but declined to back the PSOE's no-confidence vote.

Sánchez said he would lead a “pro-European” Cabinet guaranteeing “macroeconomic and budget stability.” He promised to respect the 2018 budget approved by the Popular Party with support from Ciudadanos and the Basque nationalists. This was a gesture apparently aimed at securing the support of the PNV, who had managed to secure investments for the Basque region in return for their support for Rajoy's budget bill.

The Socialist leader also secured the support of Catalan pro-independence parties PDeCAT and the Catalan Republican Left for his bid to oust Rajoy.

Although he has firmly supported the anti-independence stance of Rajoy's government, Sánchez made a nod to the secessionists by promising to promote "dialogue" between Madrid and the separatist regional government in Barcelona while respecting Spain's unionist constitution.

“You need to be aware of what you’re going to do" — Mariano Rajoy's outgoing plea

Rajoy leapt on such apparent contradictions in Sánchez’s remarks to Congress, reminding him that not long ago he accused the new Catalan regional president, Quim Torra, of “xenophobia.”

“We don’t know how you’ve convinced your partners to vote for you. We do know that it won’t be good for Spain,” said Rajoy.

However, in what looked like an admission that defeat was possible, Rajoy warned lawmakers in Congress: “You need to be aware of what you’re going to do.”

At the end of his speech, Sánchez told Rajoy that he still respects him, adding: “I wish you the best.”

Read this next: Trump's global trade war