But Sunday’s election did not result in a repeat of the near-record 76 percent turnout for the national election on April 28, when concerns about the emergence of Vox also helped mobilize left-wing voters.

In fact, far-left incumbent mayors lost tight elections in Spain’s two largest cities, Madrid and Barcelona. In Madrid, the capital, Manuela Carmena, a 75-year-old former Communist and retired judge, said she would resign after winning the most votes on Sunday, but not enough to prevent a right-leaning coalition from ousting her from city hall.

In Barcelona, the regional capital of Catalonia, another far-left mayor, Ada Colau, was narrowly defeated by a separatist politician, Ernest Maragall, representing the Esquerra Republicana party. The election was seen as a bellwether for the Catalan secessionist movement, almost two years after its botched attempt to declare independence. Manuel Valls, a former prime minister of France who was born in Barcelona, came in a distant fourth.

The Socialists secured 20 of the 54 seats granted to Spanish lawmakers within the European Parliament, according to preliminary results, up from the 14 seats that the Socialists had previously held.

“It’s tempting to make generalizations about European politics and talk about the rise of the populists and the eurosceptics and how voters use the European elections to punish their governments,” said Aleksandra Sojka, a Polish fellow in political sociology at the Carlos III University in Madrid. “But there are clearly exceptions — and one of them is Spain.”.

Still, the European Parliament election has also put Spain’s territorial conflict in Catalonia into the spotlight, and Sunday’s vote is likely to be followed by a legal dispute over whether indicted Catalan separatists can seek European parliamentary immunity after winning a seat in the European assembly.

[What Nigel Farage’s big win means for Brexit.]

A former leader of Catalonia, Carles Puigdemont, was elected on Sunday to the European Parliament, as was his former deputy in the Catalan government, Oriol Junqueras. Mr. Puigdemont has been living in Brussels to avoid prosecution in Spain, while Mr. Junqueras is among the jailed politicians who are now on trial before the Spanish Supreme Court. Spanish prosecutors want to sentence Mr. Junqueras to 25 years in prison on charges of rebellion and other crimes for helping lead Catalonia’s failed attempt to secede in 2017 after staging an unconstitutional referendum.

Sunday’s election puts into question the survival of Pablo Iglesias as leader of the far-left Unidas Podemos party. Having transformed Podemos into Spain’s third-largest party in 2015, Mr. Iglesias has more recently presided over the fracturing of his party as well as election setbacks.