Once ostracised by his own party, Sanchez will be next prime minister after successful bid to remove Mariano Rajoy
Stunning comeback for Spain's Socialist leader Pedro Sanchez
Less than two years ago, the man who will become Spain's new prime minister was staring at the premature end of an unremarkable political career.
The heavyweights of Spain's Socialists had forced Pedro Sanchez's removal as their leader. Back-to-back losses by the party in general elections had left Mr Sanchez without credit and he was ostracised after a rebellion from within his own ranks.
Fast forward to Friday: Mr Sanchez is set to become the leader of the eurozone's fourth leading economy after completing an audacious bid to oust conservative Prime Minister Mariano Rajoy from power in a no-confidence vote.
Mr Sanchez completed his spectacular turnaround after seizing the opportunity provided by last week's court verdict in a massive corruption case involving Mr Rajoy's conservative People's Party. His appeal for a government clean of scandal, coupled with a promise to hold new elections soon, brought him just enough votes in parliament to end Mr Rajoy's six and a half years in charge.
In October 2016, Mr Sanchez looked finished. He had lost a bid to form a government and been cast out by the party's regional chiefs. He then gave up his seat in parliament when the Socialists' caretaker leadership opted to allow Mr Rajoy to stay in power, avoiding new elections they feared would result in even bigger losses.
Mr Sanchez, however, refused to go quietly. Even so, his vow to "get in my car and visit every corner of Spain to win back" the party sounded quixotic given his scarce support among its higher echelons.
But Mr Sanchez, 46, embraced the role of underdog and tapped into the anger of rank-and-file members who felt that he had been unfairly dumped by the party elite.
He earned a stunning victory to return as leader in May last year when he won an internal party election against Susana Diaz, the candidate anointed by the party's powerbrokers, including former prime ministers Felipe Gonzalez and Jose Luis Rodriguez Zapatero.
Still, Mr Sanchez, a basketball player in his youth, had never shown the political savvy to challenge Mr Rajoy.
A political survivor and adroit parliamentarian, Mr Rajoy had apparently cleared the biggest hurdle facing his minority government when he passed a national budget recently and said he was confident he would to see out the remaining two years of his term.
That was until last week's ruling by the National Court that delivered hefty prison sentences to 29 business people and ex-members of Mr Rajoy's party, including some elected officials, for fraud, money laundering and tax evasion, among other crimes.
Mr Sanchez, often criticized as lacking a feel for the moment, pounced and left legislators with the choice of keeping a party thoroughly tarnished by corruption in charge or making a fresh start.
In parliament on Thursday, Mr Sanchez presented his case that Mr Rajoy must go because Spain "is sick and tired of serial corruption".
"Today I have returned to this chamber for three reasons, to act in accordance with my beliefs, out of a sense of responsibility and for the sake of our democracy," he told MPs.
The move to topple Mr Rajoy comes with Mr Sanchez's Socialists trailing both the People's Party and the upstart center-right Ciudadanos (Citizens) in polls.
By moving into La Moncloa palace, the seat of government in Madrid, Mr Sanchez will recover the spotlight that he had been sorely missing since leaving parliament.
He can try to leverage his position to recharge the Socialists' support, which has been eroded by the rise of both Ciudadanos and the far-left Podemos (We Can), while he picks the most opportune moment to carry through on his pledge to call new elections.
In the meantime, he will face the pitfalls of being in charge of a minority government that will have a very tough time getting anything done. Mr Rajoy has already warned of the "political instability" a Sanchez-led government would suffer.
Mr Sanchez may also pay a heavy price for taking down Mr Rajoy. In order to secure votes against Mr Rajoy from Catalonia's separatists, Mr Sanchez had to promise to open talks with the secessionists about the future of the north-eastern region.
Mr Sanchez had been Mr Rajoy's most loyal backer in his takeover of Catalonia's regional government following its failed secession attempt last year. Now, he runs the risk that both the People's Party and Ciudadanos will label him a traitor.
An economist by education, Mr Sanchez was chief of cabinet to the UN envoy to Bosnia, Carlos Westendorp, in the late 1990s. In 2003 he took a position as a city council member in Madrid and then climbed up the Socialist party ladder, first becoming an MP and then party leader in 2014.