The result on Sunday brings the far right into central government for the first time since 20th-century dictator Francisco Franco.
Prime Minister Pedro Sanchez, who has governed with fragile minority coalitions since 2018, said in a speech he had called PP leader Alberto Nuñez Feijoo to congratulate him.
"It's a sad night for the country," a sombre-looking Sanchez told supporters, some of whom were crying.
"We recognise and accept the result ... but it doesn't mean we will succumb to passiveness and despair."
The Popular Party, or PP, declared victory on X.
"The PP is going to win the elections and Alberto Núñez Feijóo is going to be Prime Minister of Spain."
A large crowd of supporters gathered in front of the PP's headquarters in Madrid, waving both the party's and Spain's flag.
With 98 per cent of votes counted, the PP was projected to win 135 seats with Vox winning 52, according to the interior ministry.
The ruling Socialists were projected to win 115 seats.
A party or bloc needs 176 seats in the 350-member parliament to form a government. Voter turnout of 79.6 per cent was the highest since 1982.
The vote is the first of several European elections in the next year that could bring further gains for far-right parties, starting with France in April.
Sanchez had called an early election, saying he needed a majority after parliament rejected decrees he proposed to address a mounting housing crisis, cited as Spaniards' top concern in a recent Centre for Sociological Research poll.
But his image was tarnished by a series of corruption scandals involving his inner circle and family as well as his handling of crises including two deadly train crashes, a migrant surge in the North African enclave of Ceuta and a nationwide power outage.
US President Donald Trump, an ally of Vox leader Santiago Abascal who has clashed with Sanchez over NATO defence spending and immigration, celebrated his defeat.
"He lost? Oh, that's a good thing, he did a terrible job, he deserves to lose," Trump told reporters at the White House.
People's Party supporters poured into the streets and side streets around the the party's headquarters in Madrid, clapping, whistling, and waving PP and Spanish flags as the right-wing victory became ever clearer.
As the evening progressed, more supporters continued to arrive and volunteers began handing out Spanish flags outside the headquarters.
While negotiations between the PP and Vox over the distribution of government posts are expected to be difficult, the two parties already have experience working together in governing coalitions in four Spanish regions.
Coalition talks face no formal deadline before the first investiture vote, but if no candidate wins parliament's confidence within two months of that attempt, fresh elections are automatically triggered.
"Mission complete!" Vox leader Santiago Abascal shouted to a thrilled crowd of supporters, who responded by shouting insults against Sánchez. "
"We have kicked them all out!"
The new lower house will meet on December 23 and elect a speaker.
King Felipe will then nominate a candidate for the premiership, usually the leader of the party with the most votes.
The PP says it would prohibit mass amnesties for migrants, allow for returns to third countries and introduce a points system for legal immigrants.
Vox wants to deport convicted foreigners and give priority to Spaniards in the queue for housing.
Vox also backs lower taxes while opposing climate policies and expanded LGBTQI rights.
Democracy was restored in Spain in 1978, three years after the death of Francisco Franco, who ruled the country for almost four decades after a brutal civil war from 1936 to 1939.
with AP