Spain was thrust into political uncertainty on Sunday after national elections left no party with enough support to form a government, most likely resulting in weeks of horse trading or potentially a new vote later this year.

Returns showed most votes were divided between the center right and center left. But neither the governing Socialist Party of Prime Minister Pedro Sánchez nor his conservative opponents won enough ballots to govern alone in the 350-seat Parliament.

While the conservatives came out ahead, the allies they might have partnered with to form a government in the hard-right Vox party saw their support crater, as Spaniards rejected extremist parties.

The outcome was an inconclusive election and a political muddle that has become familiar to Spaniards since their two-party system fractured nearly a decade ago. It seemed likely to leave Spain in political limbo at an important moment when it holds the rotating presidency of the European Council as it faces down Russian aggression in Ukraine.

With 99 percent of the returns in, the conservative Popular Party won 136 seats in Parliament, compared with 122 for the Socialists. But they had hoped to win an absolute majority and govern without Vox, which many of the party’s own officials consider anachronistic, anathema to Spain’s moderate values and dangerous.

“I feel very proud,” the party’s leader, Alberto Núñez Feijóo, said shortly after midnight, arguing before a crowd waving Spanish flags that since his party won the election, he had the right to form a government.

But his speech had a clearly defensive tone, and he said that the candidates who have won the most votes have always governed, arguing that it would be an “anomaly” if it didn’t happen this time, and would tarnish Spain’s reputation abroad. He said his goal was to spare Spain a period of “uncertainty.”

Outside, the lyrics “Tonight’s going to be a good night” echoed amid a celebratory atmosphere, but supporters understood that it was not really a good night for their party.

“I thought they were going to win big,” said Isabel Ruiz, 24, who wore a Spanish flag over her shoulders. She said she was prepared to keep voting to get rid of Mr. Sánchez.

A political mess is not new to Spain. In 2016, the country spent 10 months in political limbo as it careened from election to election. Then Mr. Sánchez ousted the conservative prime minister and gained power in a parliamentary maneuver in 2018. More elections followed until Mr. Sánchez ultimately cobbled together a minority government with the far left and support in Parliament from small independence parties.

This time, Mr. Sánchez, a political survivalist of the first order, once again defied expectations, increasing his party’s seats in Parliament and gaining enough support with his left-wing allies for now to block the formation of a conservative government.

“The Spanish people have been clear,” he said Sunday evening outside his party’s headquarters, arguing that a larger number of Spaniards wanted to stay on the progressive track.

The prime minister could potentially win another term if all the available parties opposed to the Popular Party and Vox backed him — an extremely difficult task.

“The reactionary bloc has failed,” Mr. Sánchez said.

In the weeks leading up to the election, Mr. Sánchez and his left-wing allies raised fears about his conservative opponents’ willingness to ally with Vox, potentially making it the first hard-right party to join the government since the dictatorship of Gen. Francisco Franco nearly 50 years ago.

The prospect of Vox sharing power in government unnerved many Spaniards and sent ripples through the European Union and its remaining liberal strongholds, surprising many who had considered Spain inoculated against political extremes since the Franco regime ended in the 1970s.

Vox’s ascension, liberals argued, would amount to a troubling watershed for Spain and yet another sign of the rise of the right in Europe. Instead, Vox sank, and may have brought down the chances for the Popular Party to govern with it.

Mr. Sánchez, who has governed Spain for five years, will remain as leader of a caretaker government as the composition of a new government, or timing of new elections, is worked out.

Analysts have noted that Spain’s voters had grown tired of the extremes of the right and left and had sought to return to the center. A new election, they said, would continue that trend, and most likely further marginalize Vox’s influence. The Popular Party hopes that it would take back their votes and grow large enough to govern on its own.

A progressive darling of the European Union, Mr. Sánchez presided over an economic rebound, but he alienated many voters by backtracking on promises and building alliances with political parties associated with the Catalan secessionists as well as former Basque terrorists who also once sought to split from Spain.

“I had a hard time deciding up to the last minute,” said Arnold Merino, 43, who voted for the Popular Party. “People didn’t trust him.”

Mr. Sánchez called the elections early — they had been scheduled at the end of the year — after a bruising in local and regional elections in May.

In the closing days of the race, the Socialists and the far-left umbrella group, Sumar, projected optimism about the possibility of turning things around as polls showed them trailing. Billboards around Spain showed Mr. Sánchez looking youthful and suave under a sign for “Forward” next to black-and-white pictures of the conservative leaders reading, “Backward.”

The Popular Party ran less on policy proposals than against Mr. Sánchez. Both the conservatives and their hard-right allies ran a campaign sharply critical of Mr. Sánchez, or a style of governing they called “Sanchismo,” saying he could not be trusted as he broke his word to voters, made alliances with the far left and cut electorally advantageous deals that put his own political survival ahead of the national interest.

Even so, Spain seemed in recent years to be a bright spot for liberals. Mr. Sánchez kept inflation low, reduced tensions with separatists in Catalonia and increased the economic growth rate, pensions and the minimum wage.

But the alliance between Mr. Sánchez and deeply polarizing separatists and far-left forces fueled resentment among many voters. The entire campaign, which included Mr. Sánchez and his far-left ally warning against the extremism of Vox, turned on the bad company of the main parties’ allies.

And yet, for all the talk about extremism, results showed that Spanish voters, many of whom were haunted by the dictatorship and the decades of terrorism spawned by related territorial disputes, turned to the center.

The Vox party, widely seen as a clear descendant of Franco’s dictatorship, lost 19 seats. It ran on opposition to abortion and L.G.B.T.Q. rights and European Union meddling in Spanish affairs, and is staunchly anti-immigrant.

“I think people want to go back to bipartisanship, because it provides stability,” said Mr. Merino. “With the Popular Party, you know what you are getting.”

The leader of Vox, Santiago Abascal, split from the Popular Party amid a slush-fund scandal in 2013. Vox started with stunts like draping Gibraltar, the southern tip of the country controlled by Britain since 1713, with a Spanish flag.

It filmed alternate realities in which Muslims imposed Shariah law in southern Spain and turned the Cathedral of Cordoba back into a mosque. In another video, scored to the soundtrack of Lord of the Rings, a cultural touchstone for Europe’s new hard right, Mr. Abascal leads a posse of men on horseback to reconquer Europe.

“It’s very allegoric, but it’s also beautiful,” said Aurora Rodil, a Vox deputy mayor of the southern town of Elche who already governed with the Popular Party mayor. “There’s so much to be reconquered in Spain.”

Sunday’s vote, however, suggested that they had been beaten back.

“Spain is really balanced,” said Ramon Campoy, 35, as he took a break from work on Friday in Barcelona, standing under the L.G.B.T.Q. flag in a square graced by an equestrian statue of Ramon Berenguer III, the crowned 11th-century ruler of Catalonia.

Mr. Campoy added, “I think the country is really in the center.”