Spain's interior minister, Angel Acebes, conceded late on Sunday evening that his ruling Popular party had lost the elections.
"The PSOE has won the elections with 43.01% of the vote," Acebes said.
He added he would give the Socialists 164 seats in the 350-member Chamber of Deputies, while the Conservatives would have only 148 seats.
The Conservative defeat came in the highly emotional aftermath of last week's Madrid railway massacre in which 200 people were killed.
The conservative Popular party of outgoing Prime Minister Jose Maria Aznar, which was ahead in the polls only a week ago, apparently took a pasting because of its support for the United States in the invasion and occupation of Iraq.
Only a week ago, four public opinion polls predicted the Popular party would win with a reduced majority.
The elections came three days after a series of bomb blasts on four Madrid commuter trains that provoked high emotions against the government among many voters.
The government's support for the US-led occupation of Iraq apparently proved a liability after a statement attributed to al-Qaida said this was the reason for carrying out the devastating attacks in which 1500 people were wounded.
Up to 90% of the population was against the Iraq war and occupation to which Aznar has contributed 1300 Spanish troops.
The Madrid train blasts cast a
long shadow over the elections
Aznar was stepping down after two terms as prime minister, having handed over the party reins to former deputy Mariano Rajoy, who was up against PSOE rival Jose Luis Rodriguez Zapatero.
About two-thirds of Spain's 35 million voters turned out, a relatively high proportion.
The outgoing government had laid most suspicion for the attacks on the Basque separatist group ETA, which denied responsibility.
But in the hours before the election, attention swung to possible al-Qaida involvement after a video was discovered claiming the atrocity in the Islamist group's name.
"We claim responsibility for what happened in Madrid exactly two and a half years after the attacks in New York and Washington," said a man claiming to be an al-Qaida spokesman in the video.
"This is an answer to your cooperation with the Bush criminals and their allies. This is an answer to crimes which you committed in the world, notably in Iraq and Afghanistan, and there will be more, so help us God."
After the threat, Spanish authorities implemented "exceptional" security measures for the elections with 106,000 officers in the streets and additional police forces "ready to be sent anywhere they may be needed", an interior ministry spokesman said.
Three Moroccans and two Indians arrested late on Saturday in relation to clues found in a bag containing a bomb that failed to go off, were being held under an anti-terrorist law permitting extended detention.