PM Pedro Sánchez’s party polled 29% and will need the help of either left-wing Podemos and regional parties, or the centre right, to form a government.
Far-right party Vox also won seats – the first time a significant far-right force has done so in decades.
Vox opposes multiculturalism, unrestricted migration, and what it calls “radical feminism”.
Analysts say support for Vox has been boosted by widespread anger at separatists in the province of Catalonia, who want independence from Spain. Vox fervently opposes any concessions to the secessionists.
The other big story of the election was the collapse in support for the conservative Popular Party (PP), which governed Spain until it was dumped from power in May 2018 in a no-confidence vote.
In its worst election ever, the PP won just 66 seats, down from 137 in the previous parliament.
Turnout was 75.8%, the highest for several years and 9% more than the previous election in 2016.
In his victory speech, Mr Sánchez said the party’s big challenges were to fight inequality, advance co-existence and halt corruption.
“The future has won and the past has lost,” he told cheering supporters. During his time in office he has raised the minimum wage, appointed a female-dominated cabinet and promised to strengthen rape laws, defining it as sex without clear consent.
What just happened?
After weeks of Spain’s resurgent far right hogging all the headlines, didn’t the centre-left just win a resounding victory?
Did Spaniards have a last-minute change of heart? What does this all mean?
Spain’s Socialist party members will certainly have the biggest smiles on their faces this morning. But landslide victory this was not.
The party improved massively on its last performance in national elections. It managed to take control of Spain’s upper house of parliament too, but still lacks a majority to govern.
What happens next?
The result is a personal success for the prime minister, who increased his party’s share from 23% of the vote in 2016.
But it still leaves the Socialists and Podemos 11 seats short of the necessary 176 for a majority in the 350-seat parliament.
Mr Sánchez must now look for support from the smaller parties or from the centre right.
An alliance with centre-right Ciudadanos (57 seats) would give him the numbers, but its leader, Albert Rivera, was bitterly critical of Mr Sánchez’s collaboration with Catalan separatists, referring to his earlier “Frankenstein government” – and vowed he would not enter a coalition with the Socialists.
And as Mr Sánchez gave his victory speech late on Sunday, supporters chanted “Not with Rivera!”, making it clear they also did not want a coalition with Ciudadanos.
It was a big night for Ciudadanos leader Albert Rivera after the party’s best result so far
The head of previous coalition partner Podemos, Pablo Iglesias, made clear on Sunday night that another left-wing administration was possible.
But the results appear to make any left-coalition dependent on Catalan pro-independence parties, which opponents on the right see as toxic.
An alliance involving all the other regional parties, including the Basque separatist PNV, would leave him one seat short of a majority.
All parties are now looking towards the regional and European elections in less than a month.
Who are Vox?
The PP’s historic defeat means that, even with the support of Ciudadanos (Citizens) and Vox, it has no chance of forming a right-wing coalition, and leader Pablo Casado said it would become the main opposition force.
But Vox saw its support surge, winning more than 10% of the vote, which gives it 24 seats.
It received congratulations from other right-wing leaders, including Italy’s deputy PM Matteo Salvini and Marine Le Pen, leader of the French far-right National Rally.
Vox’s success is seen as a turning point for the far-right, who have not won seats in parliament since the death of Spanish dictator Francisco Franco in 1975 and the restoration of democracy – with the sole exception of a single seat for Blas Piñar in the first parliament of 1979–1982.
Led by Santiago Abascal, a former PP member, the party has emerged in a matter of months with a vow to “make Spain great again”.
In his speech after the results, Mr Abascal said: “We told you that we were starting a reconquest of Spain and that is exactly what we have done… and we can clearly say to all of Spain that Vox is here to stay.”
Vox rejects the far-right label but its views on immigration and Islam place it in line with far-right and populist parties elsewhere in Europe.
Media captionWho are Spain’s far-right party?
It wants to repeal laws against gender violence, and opposes abortion and same-sex marriage. Critics see it as a nationalist throwback to the Franco era.
Vox aims to deport migrants legally entitled to be in Spain if they have committed an offence, and wants to prevent any migrant who comes in illegally from staying.
Why is Catalonia so important?
The future of Spain’s semi-autonomous north-eastern region was one of the big issues of the election.
Catalonia held an independence referendum in October 2017 and then declared independence from Spain.
A dozen of its leaders have since gone on trial in Madrid, facing charges including rebellion and sedition.
During last Monday’s election debate, the leaders of the PP and Ciudadanos accused the prime minister of siding with “enemies of Spain” and wanting to “liquidate Spain”.
The centre-left Catalan ERC was the big winner in Catalonia, with a projected 15 seats. Its leader, Oriol Junqueras, is among those facing trial.