Spain's ruling conservatives have held their ground in municipal elections - a poll seen as the first test of national sentiment since the government's controversial support for the US-led war on Iraq.
Mr Aznar's party keeps power in key regions and cities
Opposition Socialists had hoped to tarnish Prime Minister Jose Maria Aznar's Popular Party for backing the war and also for his handling of the Prestige oil tanker spill last November which became Spain's worst environmental incident.
With virtually all the votes in Sunday's poll counted, the Socialists took 34.7% of the vote, about 200,000 votes ahead of Mr Aznar's party which finished on 33.8%.
But that was a better than expected result for the Popular Party which retained power in key areas.
The party won in nine of 13 regions and in 35 out of 52 cities, including the powerful mayorship of the capital Madrid.
Mr Aznar was jubilant as he addressed supporters in Madrid.
This is a good beginning in regards to 2004 [general elections]
Socialist Party leader Jose Luis Rodrigues Zapatero
"Some thought they could sweep the Popular Party off the map, but today we're stronger and more vigorous than ever," the prime minister said.
"You've scored sensational results," he added.
Mr Aznar's party lost the northern city of Zaragoza but retained absolute majorities in the key cities of Valencia, Malaga and Valladolid.
The Socialists - who won more votes than the Popular Party for the first time since 1993 - also claimed victory.
"This is a good beginning in regards to 2004," the Socialists' leader Jose Luis Rodrigues Zapatero said, referring to general elections, in which Mr Aznar has said he will not stand.
The turnout was 67%.
Poll ratings slump
The results of the poll are much better than expected for the prime minister.
The Socialists got the best results in the south of the country
The Iraq war dominated the two-week political campaign.
Mr Aznar defied public opinion with his support for the US, seeing his approval rating fall to 31% and the Socialists overtake his party in opinion polls for the first time since he took office in 1996.
In the vote, Socialists did best in their traditional stronghold in southern Spain including the provinces of Andalucia and Extremadura.
Spain's third-biggest party - the United Left - lost ground, wining about 6.0% of the vote compared with 6.52% in 1999.
Regional parties dominated in the Catalan capital of Barcelona - the country's second largest city - and also in the Basque region.
But hundreds of pro-independence candidates accused of links to the Basque militant organisation ETA were disqualified before the elections by Spanish courts.
Basque separatists were banned from standing in the poll
Their supporters - as predicted by analysts - seem to have switched to the moderate nationalists, led by the Basque Nationalist Party.
The party won the biggest number of seats in the northern town of Bilbao.
But the ETA separatists still claimed about 150,000 votes - or 10% in the Basque region - after they distributed phoney ballots for supporters to cast a symbolic vote that would be nullified.