His Popular Party (PP) was the big winner of the election, the second in six months, which played out amid the turbulence from Britain's shock vote to leave the European Union.
The party, which portrayed itself as the guardian of stability, won 137 seats in the 350-strong lower house of parliament -- 14 more than in December and more than what pre-election polls predicted.
The Socialists held on to second place like in December, though with just 85 seats, their worst score in modern history.
The far-left Unidos Podemos coalition came third with 71 seats, reversing pre-election expectations that it could leapfrog over the Socialists and replace them as the main left-wing force.
More From This Section
Market-friendly party Ciudadanos, meanwhile, finished fourth as in December with 32 seats, down from 40, as many of the party's voters moved back to the PP.
"We won and we demand the right to govern," Rajoy said as he looked down from a tall podium on a crowd of supporters waving blue flags and shouting "yes we can!" -- stealing Podemos' key catchphrase.
But while the PP boosted it's seats, it still faces that same challenges to form a government as after the December polls when Podemos and Ciudadanos uprooted the country's two-party dominance.
Without a majority, it will need to seek the outright or tacit support of other parties to get a coalition or minority government through.
The combined total number of seats if PP teams up with Ciudadanos, their natural ally, is not enough to form a majority centre-right government so they may need to court the Socialists too or smaller regional parties.
The general election in December resulted in a parliament so splintered that parties failed to agree on a coalition, and this is what prompted Sunday's repeat vote.
After failing to form a government after the last polls, political leaders will be under more pressure this time to form some sort of government.