The Socialist Party won 123 out of 350 seats in parliament with about 29 percent of votes, leaving it short of a majority but well ahead of the 85 seats it won in the last election in 2016.
Sanchez came to power in June following a no-confidence vote against conservative Prime Minister Mariano Rajoy, whose Popular Party took 66 seats compared to the 137 it won in the previous poll.
“The Socialists have won the general election, and with it, the future has won and the past has lost,” Sanchez told supporters at party headquarters in Madrid late Sunday.
But the result means Sanchez will need to form a coalition with hostile forces in order to govern.
For the past ten months, he governed through alliances with hard-left Podemos or separatist parties in Catalonia, whose refusal to back Sanchez’s 2019 budget led to Sunday’s snap polls.
Sanchez could also seek a deal with centre-right Ciudadanos (Citizens), whose 57 seats would secure an absolute majority. However, supporters of both parties would likely frown upon such a deal.
Ciudadanos leader Albert Rivera built his campaign on disparaging Sanchez, namely criticizing his attempts to negotiate with separatists parties from Catalonia in a bid to ease the north-eastern region’s secessionist crisis.
Far right surges on criticism over Catalan separatists
The crisis in Catalonia, which came to a head in an unauthorized independence referendum in 2017, also fuelled the rise of ultra-nationalist party Vox in the southern region of Andalusia.
During the campaign, Vox advocated the “defense of the Spanish nation to the end” and a hard line against Catalonian separatists.
The party took just over 10 percent of the ballot on Sunday, meaning Spain has its first significant far-right political force since the death of dictator Francisco Franco in 1975.
Opinion polls had predicted greater results, but party leader Santiago Abascal was more optimistic. “We can tell Spain with a complete calm that Vox has come to stay,” he told supporters.
Other right-wing parties attacked Sanchez for talking with separatists that they accuse of trying to break up Spain.
The instability provoked by Catalan separatism and its aftermath is likely to continue shaping Spanish politics, with two pro-independence parties growing their shared presence in parliament from 17 to 22 seats on Sunday.
The five newly elected separatists are in jail and are currently being tried at Spain’s Supreme Court.