As the country headed towards an unprecedented third election in less than a year, its political fate hinged on whether the Socialists would allow a Rajoy-led minority government to rule and avoid more polls, and on Sunday they swallowed a bitter pill and voted to do so.
While conservatives cautiously welcomed the move, it will come as a blow to millions of Spaniards who voted for two upstarts they thought could bring change - far-left Podemos and centrists Ciudadanos - and many Socialist supporters.
"An important decision was taken yesterday, and in my opinion a reasonable one," Rajoy tweeted Monday with his usual reserve, as the main Ibex 35 index of Spain's stock market shot up 1.44 percent early afternoon on news the country would finally get a government.
The decision caps a 10-month period that saw Spain go from jubilant hope after December 2015 elections ended the traditional two-party system to disillusion following repeat polls in June.
Back in December, millions of voters fed up with austerity and corruption during Rajoy's four-year term had cast their ballot for Podemos and Ciudadanos, led respectively by the 38-year-old Pablo Iglesias and Albert Rivera, 36.
This resulted in a fragmented parliament where no grouping had enough lawmakers to govern alone, even if Rajoy's Popular Party (PP) won the most seats.
But party leaders failed to reach any kind of viable coalition deal, prompting repeat elections in June with a similar result.
Rajoy will be weak
Now Rajoy - whose perceived dullness and mistrust of the media earned him the nickname "plasma prime minister" after he conducted press conferences via video screen - is poised to rule again thanks to the Socialists.
On Sunday, after weeks of in-fighting that led to the resignation of Socialist chief Pedro Sanchez, 44, they voted to abstain in a parliamentary confidence vote on a PP government - which would give it enough traction to get through the vote.
"The great winner is Mariano Rajoy," said Anton Losada, politics professor at the University of Santiago de Compostela.
"The headline could be: 300 days later, the boring plasma prime minister knocks out the three young emerging leaders who had come to eat him alive."
On Monday, King Felipe VI started a round of talks with party leaders - an obligatory stage in the post-election process and the fifth to take place since December.
He meets Rajoy on Tuesday and will almost certainly designate him as prime ministerial candidate, safe in the knowledge that his minority government will succeed, even if rebel Socialist lawmakers threaten to vote no.
Two parliamentary debates and confidence votes will subsequently be called - the final one expected to take place on Saturday or Sunday, and Spain due to have a government by November 1st.
But with just 137 lawmakers out of 350, Rajoy will not have an easy ride, given the huge opposition in parliament.
"It's unprecedented in Spain to have a government with so little parliamentary support," said Fernando Vallespin, politics professor at the Autonomous University of Madrid.
"He will have to negotiate every single law."
Still, he may be able to count on a divided opposition.
The Socialists have been ripped apart by in-fighting while Podemos has also been marked by divisions - and both strongly distrust each other.