Sanchez, a 46-year-old economist with no government experience who has made a spectacular comeback to the frontline of politics, took the oath of office before King Felipe VI in the Zarzuela Palace near Madrid.
“I promise to faithfully fulfil the duties of the post of prime minister with conscience and honour, with loyalty to the king, and to guard and have guarded the constitution as a fundamental state rule,” he said in the presence also of Rajoy, without a Bible or crucifix — the first to do so.
The Socialist leader must still name his cabinet and it is only when their names are published in an official government journal in the coming days that he will fully assume his functions.
His ousting of EU-friendly Rajoy, a 63-year-old veteran politician who had been in power since 2011, comes at a time of political instability in Europe as Italy brings in a new eurosceptic anti-establishment government.
But even if he will head up a minority government with support of a hodgepodge of disparate parties like far-left Podemos and Catalan separatists, Sanchez has promised his “main priority” will be to respect Madrid's deficit reduction commitments to the European Union.
He has also vowed to implement the 2018 budget designed by Rajoy's conservative Popular Party (PP) government.
His arrival at the prime minister's office represents an astounding comeback for the man who led the Socialists to two crushing general elections defeats in 2015 and 2016, and was forced out by the party apparatus.
That was short-lived, though, as party activists re-elected him as party head in primaries in May 2017, but even then the Socialists were often sidelined as Podemos, centre-right Ciudadanos and Rajoy's PP took centre stage
That all changed on May 25 when the Socialists filed a no-confidence motion against Rajoy, a day after a court found former PP officials guilty of receiving bribes in exchange for awarding public contracts in a vast graft scheme between 1999 and 2005.
Other opposition parties lined up against Rajoy, who was abandoned by his allies too. An absolute majority of 180 lawmakers voted for the motion on Friday to loud applause and shouts of “Yes we can”.
“It's been an honour — there is none bigger — to have been Spain's prime minister,” Rajoy told parliament minutes before.
Tough road ahead
In his first comments after winning the no-confidence motion, Sanchez, a former basketball player, vowed to tackle “all the challenges which the country faces with humility”. But he will struggle to govern as his Socialists have just 84 seats in the 350-seat parliament.
All of his allies in the no-confidence motion stressed their vote against Rajoy was not a blank cheque for Sanchez.
“Our 'Yes' to Sanchez is a 'No' to Rajoy,” is how Joan Tarda of Catalan pro-independence party Esquerra Republicana de Catalunya (ERC) put it in parliament.
Sanchez will only be able to implement policy initiatives “that allow him to obtain an easy majority” in parliament, said Fernando Vallespin, political scientist at the Autonomous University of Madrid.
PP lawmaker Rafael Hernando said Sanchez would be entering the prime minister's office “through the back door” after failing to win any general elections.
Sanchez has already tied his hands by promising to respect Rajoy's 2018 budget, which includes generous concessions to the northern Basque region.
He has also said he wants to “build bridges” with Catalonia's new separatist government, headed by Quim Torra, which will take office on Saturday.
The parties that supported Sanchez will make demands he will not meet, predicted Pedro Fernandez, a 68-year-old pensioner, outside of parliament.
“When he does not do what they want, they will remind him that they brought him to power. And in five or six months we will either have fresh elections or they will oust him,” he added.