With 100 percent of the vote counted, the ruling Prime Minister Jose Luis Rodriguez Zapatero's Socialist party had 34.9 percent against 35.6 percent for the right wing Popular Party led by Mariano Rajoy R, a difference of 155,991 votes.
The Socialists, however, won 679 more town hall council seats than the Popular Party out of a total of 66,162 that were up for grabs.
"Rajoy wins the primaries," the conservative La Razon daily said in its front-page headline, while leading Socialist-leaning daily El Pais said: "Socialists win power but Popular Party strongest in votes after landslide in Madrid."
In regional voting also held Sunday in 13 of 17 regions, the two parties retained most of the respective strongholds, although the conservatives saw their majority in Navarra, a region bordering the Basque country, slip into jeopardy due to a strong rise by a Basque nationalist party.
The voting was the first nationwide voting test for Zapatero since he won general elections in March 2004 after the Madrid train bombings by suspected Islamic militants. Sunday's election was seen as a rehearsal for the next general elections scheduled for March 2008.
Together with Syrian soldiers, Russian fighters will have to ensure the creation of a corridor to withdraw civilian population