Jose Luis Rodriguez Zapatero's ruling Socialist Party has won Spain's general election.
Supporters flocked to the Socialist Party headquarters to celebrate
With nearly all the votes counted, the socialists had won 169 seats, short of the 176 needed for an absolute parliamentary majority.
Mariano Rajoy's conservative Popular Party was set to win 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 Socialist Party's Madrid headquarters.
The BBC's Johnny Dymond in Madrid said his tone was emollient, after what has been a bad-tempered battle for votes.
Party spokesmen said the result was a clear endorsement of the prime minister's programme of liberal reforms, which have been fiercely opposed by conservatives and the Roman Catholic Church.
Mr Rajoy, the prime minister's leading rival, 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.
SEATS WON IN LOWER HOUSE
PSOE (Socialists): 169
PP (Popular Party): 153
CiU (Catalan): 11
PNV (Basque Nationalist): 6
ERC (Catalan leftist): 3
IU (United Left): 2
BNG (Galician Nationalist): 2
CC (Canary Isles): 2
UPyD (Progress and Democracy): 1
NA-BAI (Navarre): 1
Source: Reuters, with 99.7% of votes counted
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.
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."
Reporting from the Socialist Party headquarters, our correspondent said a huge roar went up from supporters as Mr Zapatero came out to address them.
Although the party has increased its number of seats from 164, he says it will still have to go into an informal coalition, probably with the largest Catalan nationalist party.
Despite attempts to attack the ruling party on the faltering economy, immigration and accusations of being soft on terrorism, the PP failed to make the charges stick, our correspondent says.
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.
In 2004, voters turned out in high numbers - galvanised by the Madrid train bombings that killed 191 people three days earlier - to give Mr Zapatero a surprise victory over the conservative government, then led by Jose Maria Aznar.
Sunday's turnout nearly matched the level of Spain's 2004 elections
However, the economy has been a key issue this time.
After a decade of growth, Spain's economy 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 Rajoy's conservative opposition party focused on immigration, a bigger issue than in previous polls.
The Socialists, meanwhile, highlighted the liberal reforms of their time in office, including the introduction of a gender-equality law, fast-track divorces and same-sex marriage.
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 remaining 56 Senate seats are decided by indirect election by assemblies in Spain's 17 autonomous regions.