Long queues formed outside many polling stations and turnout was on track to be one of the highest ever, reaching 68.1 per cent by 6pm on Sunday, according to interior ministry data, up from 53.1 per cent recorded at the same time during the previous ballot in July 2023.
Sanchez has said a turnout of 75 per cent or more would likely produce a progressive majority.
However, nearly all opinion polls before the election pointed to the conservative People's Party (PP) and nationalist populist Vox together surpassing the 176-seat threshold needed for a lower house majority.
The PP was projected to win up to 134 seats and Vox up to 48 while Sanchez's Socialists could obtain up to 124 seats, according to exit polls on Sunday by several media outlets.
In previous elections, however, results have shifted significantly from the initial projections as the night progressed.
Sanchez has earlier warned voters that a conservative government would not make their lives more affordable and frequently has said that his opponents would take the country of 50 million backward.
"I'm feeling optimistic," Sanchez told reporters after casting his vote on Sunday, saying he expected "a huge turnout".
Opposition leader Alberto Nunez Feijoo of the PP has promised voters more stability and better governance.
He has criticised Sanchez's record for his government's failure to improve Spaniards' housing woes, two fatal train crashes this year on the high-speed rail network, a country-wide blackout in April 2025 and a border crisis in Spain's north African exclave of Ceuta.
Feijoo also pilloried Sanchez - who relied on backing from progressive and separatist parties to cling to power in 2023 - for not being able to pass a budget in nearly four years, lacking parliamentary support.
"I ask Spaniards not to vote with fear but with memory," Feijoo said in Madrid after casting his ballot.
"Today is a day when the Spanish people take the helm and will decide the future, the destiny and the direction of Spain."
It is widely expected that Feijoo will attempt to form a coalition with the Vox party, led by Santiago Abascal who has campaigned on an immigration crackdown.
Vox is betting that its hard-line stance will win over voters and enable it to play kingmaker with the PP.
"Spain is deeply divided. There is a lot of fear on both sides, and I believe we need to soften the tense atmosphere and redirect what has been done wrong during the past eight years," said Paloma Jiménez-Ontiveros, a 65-year-old real estate agent in Madrid, after voting.
Sanchez's party, including members of his inner circle, has been dogged by legal cases involving corruption.
The cases have implicated a former minister, top officials and family members, eroding voters' trust.
Feijoo in contrast is a lifelong civil servant-turned-politician from rural northwestern Galicia and projects himself as a sensible, moderate and effective administrator.
Spain has welcomed millions of people, mainly from Latin America, but also from Africa and elsewhere in Europe.
Earlier this year, the Spanish government legalised the status of nearly 1.2 million immigrants living in the country without authorisation.
Feijoo and Abascal have portrayed Sanchez's immigration-friendly policies as harmful to ordinary Spaniards, pointing to the country's insufficient housing stock and strained public services.
About 39 million people are eligible to cast ballots.
Votes in Spain are tallied quickly and the result is usually known within hours but this does not mean it is immediately clear who will govern.
with DPA and Reuters