Spain's Prime Minister Jose Luis Rodriguez Zapatero has promised a new era in Spanish politics, after winning another four year term in office.
Prime Minister Zapatero jumped for joy at his victory
His Socialist Party won 169 seats, five more than in the last election, but still short of an absolute majority.
Mariano Rajoy's conservative Popular Party won 153 seats.
Mr Zapatero thanked jubilant supporters for a "clear victory" and urged unity, pledging a "new period" in Spanish politics after a bitter campaign.
"The Spanish people have spoken clearly and have decided to open a new period without tension, without confrontation," Mr Zapatero told the flag-waving crowd gathered outside his party's Madrid headquarters.
Backing for reforms
Party spokesmen said the result was a clear endorsement of the prime minister's programme of liberal reforms - including a gender-equality law, fast-track divorces and same-sex marriage - which has been fiercely opposed by conservatives and the Roman Catholic Church.
The BBC's Steve Kingstone in Madrid says the Popular Party (PP) will now have to face up to a clear defeat. It had never really got over losing in 2004, he says, which it blamed on voter reaction to the Madrid train bombings three days before the election.
The PP had looked likely to win before the 2004 attacks. But voters rebelled at what they saw as its attempts to blame Basque separatists, ignoring evidence implicating Islamists and diverting attention from its unpopular role in the Iraq war.
Mr Rajoy, the prime minister's leading rival, had accused him during the campaign of winning in 2004 "because of Iraq and the 11 March attack".
But he said after Sunday's defeat that he had congratulated Mr Zapatero.
"I have called the candidate of the Socialist Party and I have wished him luck for the good of Spain," he said.
Turnout was recorded at 75.3%, only just below the record 75.6% last time round. It had been predicted that a high turnout would benefit the Socialists.
Mariano Rajoy has now been defeated in two straight elections
Both main parties increased their percentage of the vote and their number of seats compared with the 2004 election, at the expense of small leftist and regional parties.
The Socialist Party took 43.7% of the vote, and the PP 40.1%.
Pio Garcia Escudero, the PP's campaign co-ordinator, took consolation in the party's improved performance.
"The number of votes we have won has risen considerably from 2004, as has the number of seats," which was up from 148 last time, he said. "That gives us enormous satisfaction."
Although the Socialist Party has increased its number of seats, correspondents say it will still have to go into an informal coalition, probably with the largest Catalan nationalist party.
The elections were marred by Friday's killing of the former socialist councillor, Isaias Carrasco, in the Basque country.
Police have blamed Basque separatists for the shooting, which brought election campaigning to an early close, but so far no group has claimed responsibility.
But security has been less of an issue in this election than the economy.
After a decade of growth, Spain is stuttering. Inflation is at a 10-year high and unemployment is the highest this century.
The Spanish housing boom is dwindling, exacerbated by the global credit crunch.
Mr Zapatero is expected to start his new term by announcing measures to boost growth and jobs.
Spain's 35 million voters were electing 350 members of the Cortes, or lower house of parliament, and 208 members of the 264-member upper house, the Senate.
The Socialists gained eight seats in the Senate - taking them to 89 - and the PP lost one - giving it 101.
The remaining 56 Senate seats are decided by indirect election by assemblies in Spain's 17 autonomous regions.
I voted for the PP, even though I would have prefered to vote for a real alternative if it had existed. These results just confirm the radical division that Spain suffers nowadays, it looks like we've gone 75 years back in our history. I just hope PM Mr.Zapatero and the PP leader to arrange new pacts on the important issues for Spain, and to forget the confrontation of the last four years.
Antonio, Madrid, Spain.
I voted for the Socialist Party. After having had the worst opposition party of the democratic history of Spain I congratulate for the victory of Mr Zapatero. I hope the policy of lies, false accusations as regards terrorism and separatism will finish on the conservative party and I hope they will have taken note that the Spanish citizen do not want them.
Jose Moreno, Albacete Spain
Zapatero has won the election by getting the useful vote from the extreme left (IU comunists and ERC Catalan separatists), while the center vote pushed up the Partido Popular. His talk about unity I fear is just that...talking that will not materialize in actions.
Salvador Barrio, Madrid, Spain
The victory of Mr. Zapatero clearly shows a country with hunger for reforms. Nevertheless those reforms are just a demagogy smoke screen in a country and its society, hesitant to join the most advanced economies and happy to be a few steps back. It is the paradox of Spanish society which has been like that for the last three centuries. There is no aim to lead and no drive to make bold moves on areas rather than social issues...and if that annoys the church the better.
Jose Egea, Spain
It looks as though the next four years are going to be tough. I think that the Popular Party erred in not attacking the Socialist's economic policy. Rising unemployment,high,very high inflation. More small businesses going under every day and very bleak prospects for the coming years. Mr Zapatero, in a very intelligent move,left the economis issues to his Vice President and finance Minister Mr Pedro Solbes who as the very astute and experienced politician he is,literally "wiped the floor" with the Conservative's novice candidate,former businessman Seņor Pizarro. From then on it was all up hill for Mariano Rajoy and the Popular Party.
Anthony Brown, Barcelona Spain
The PP has lost because of its four years of insults and radical policies. Thank God. We need moderate politicians who can solve the problems.
Juan Antonio, Spain
I am really happy. I voted for PSOE again, and I hope they fulfill what they promised. More rights WE have since this left-wing party has ruled our country over the last four-year term. I also hope that some problems such as housing, unemployment and the blight of terrorism will have gone down during this term. Prices are still rises but our re-elected President said the first measure he was going to take was to call unions,employers and the government to agree a social agreement to try to improve this touchy situation which affects every Spanish budget
David, Cadiz, Spain
I have lived in Spain for 14 years but was not permited to vote as I am British. I was horrified by the PSOE victory as the state of the economy has steadily declined in the last four years. Unemployment in my area is at an all time high and the people here are struggling to make ends meet. Many property owners have been forced to put their homes on the market because they cannot pay the increased mortgage interest rates. I dread to think what situation we will be in when the next elections come around.
Sara Stannard, Alicante, Spain
I think the victory for Zapatero was clear from a while ago. On one hand Rajoy is a very clear thinker who uses research and figures to back up his comments, and on the last debate he looked clearly fedup and impatient with Zapatero's comments, which made him look arrogant and detached. On the other hand Zapatero opened his arms in a very confident gesture and said:" We spanish need to unite together to make the best of our Nation...etc. The way a good politician talks. Lets hope that he can deliver!!
Sol Payne, Huelva, Spain