(Bloomberg) — Prime Minister Pedro Sanchez engineered a late swing during the final days of the Spanish election campaign to deny his right-wing opponents a majority in parliament.

Most Read from Bloomberg

While the center-right People’s Party won the most seats, the right-wing bloc has only 170 in total and needed 176 to oust the 51-year-old Socialist. With a wider range of potential partners, Sanchez could potentially muster 172 votes.

Sanchez and PP leader Alberto Nunez Feijoo both claimed victory as they addressed their supporters after the count, but it’s Sanchez who has the upper hand. That uncertainty may roil Spanish assets in the short term, according to JPMorgan Chase & Co, which last week warned that an inconclusive vote would be the worst outcome for investors.

Sanchez will remain prime minister for the time being, since Feijoo lacks the numbers to oust him, and he might even be able to piece together enough votes to earn a third term. More likely, he’ll have to call another election later in the year, but with the chance to determine the timing and shape the agenda before then.

“Sanchez, ever the political survivor, lives to fight another day,” Federico Santi, an analyst at Eurasia Group, said in an interview. “Neither the right nor the left have a clear path to a majority. Currently, the most logical conclusion seems to be repeat elections.”

Feijoo had looked set for a clear victory when a polling blackout began on Tuesday, with the final surveys suggesting the right-wing bloc would win as many as 180 seats. To turn the situation around, Sanchez embarked on a media blitz and deployed his predecessor on the campaign trail to help get the Socialist vote out.

The prime minister gave a string of interviews, including with journalists who he had avoided for years for considering them too critical. Former Socialist Prime Minister Jose Luis Rodriguez Zapatero did days of rallies and interviews, urging supporters to stop the right wing bloc from winning.

That strategy delivered strong showings in Catalonia and Andalusia, traditional strongholds for his party, where support had fallen in recent years. On Sunday, the Socialists won 24 seats for Andalusia against the PP’s 23. In Catalonia, the Socialists were the most voted party and won 19 seats against six for the PP.

Speaking from a stage outside the Socialists’ Madrid headquarters in jeans and a denim shirt, Sanchez said the right-wing bloc had been defeated.

“We had to decide whether to go forward or backward, as the PP was proposing,” he said. “Those who want Spain to go forward are many more. “

His supporters waved Socialist banners and rainbow flags — in a sign of support for the LGBTQ community that was threatened by the far-right group Vox. “No pasaran,” they chanted — “they shall not pass” — a republican slogan of resistance from Spain’s civil war almost a century ago.

Across town, the mood was more subdued outside the PP’s offices as conservatives tried to put a brave face on a victory that fell short of their expectations. Some supporters chanted the name of party rival Isabel Diaz Ayuso as Feijoo addressed the crowd, in a sign of the tensions and potential fallout the PP leader will have to manage in the weeks ahead. Diaz Ayuso was standing on the stage too.

Feijoo told supporters he’d won the right to govern and appealed to Sanchez not to stand in his way.

Vox’s leader Santiago Abascal accused the PP of complacency and said Sanchez is now in position to stay in power.

“Even though he lost the election, Pedro Sanchez could block the swearing in of new prime minister and, what’s worse, Pedro Sanchez could even be sworn in again with the support of the Communists, separatist insurrectionists and terrorists,” he said.

Sanchez could squeak through in a vote of confidence if Junts per Catalunya, a separatist party, chose to abstain but Spain is more likely heading for an extended period of gridlock.

Miriam Nogueras, a lawmaker for Junts, said the party will demand the return of exiled Carles Puigdemont, the former president of Catalonia who has been living in Belgium since the failed attempt to break away from Spain in 2017. The group will also ask for a referendum on independence, she said.

“We will not make Sanchez premier in exchange for nothing,” she said.

JPMorgan analyst Marco Protopapa wrote in a note last week that in the very short term an inconclusive result “could be the least market and growth friendly outcome for the additional uncertainty it would trigger.” Analysts had recommended buying Spanish assets ahead of the election based on projections that Feijoo would come out on top.

Sanchez has riled executives with windfall taxes on banks and energy companies and personal attacks on Ferrovial SA’s Chairman Rafael Del Pino over the company’s decision to shift its domicile and listing to Amsterdam.

The shock result vindicates Sanchez’s high-risk decision to call an early election after his party was given a kicking by voters in May’s regional elections.

Sanchez has seen the economy recover from the pandemic and employment reach a record. But the surge in prices over the past two years has left Spaniards with one of the sharpest drops in real income in Europe and some people are struggling to repay mortgages as market rates surge. The premier has promised relief to mortgage holders by extending home loan in order to reduce monthly payments.

–With assistance from Macarena Muñoz.

Most Read from Bloomberg Businessweek

©2023 Bloomberg L.P.