Spain's governing Socialists won the country's third election in four years, but have fallen short of a majority.
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.
For the first time since military rule ended in the 1970s, a far-right party, Vox, is set to enter parliament.
Vox opposes multiculturalism, unrestricted migration, and what it calls "radical feminism".
Analysts say support for Vox has been boosted by widespread anger at the Catalan independence drive. The party 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 biggest for several years and 9% higher 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 bring in laws defining rape 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, but there is no easy solution.
An alliance with 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.
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 centre-right 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.
Its success is seen as a turning point for the far-right, who have never won seats in parliament since the death of Spanish dictator Francisco Franco in 1975 and the restoration of democracy.
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".
It won seats for the first time in local elections in the southern Andalusia region, and agreed to support a centre-right coalition of the PP and Ciudadanos.
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.
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.
The BBC's James Reynolds says that fear of big gains for the far-right helped the Socialists to victory.
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. Among them is ERC leader Mr Junqueras, who was Catalan vice-president when he was arrested.
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 in jail facing trial for his role in declaring independence in October 2017 and tweeted thanks for the million votes his party received.