Sanchez, a former economics professor ousted in a messy internal rebellion last October, comfortably beat two rivals in a poll of party members, taking 50.2 percent of the vote.
His nearest rival, Andalusian regional president Susana Diaz -- who was backed by the bulk of the party's leaders and was seen as more amenable to striking deals with the government -- took 39.9 percent.
Nearly 190,000 card-carrying members of the Spanish Socialist Workers' Party (PSOE) were eligible to vote in the election, which was billed as an attempt breathe new life into the struggling 138-year-old party.
Sanchez was forced to resign over his staunch refusal to allow Prime Minister Mariano Rajoy and his Popular Party (PP) to form a minority government, following two inconclusive general elections.
Diaz and the other architects of the rebellion argued it was best to let a PP government through rather than go to the third elections in a year and risk losing even more votes.
Sanchez called this a huge mistake. He was deeply critical of corruption scandals impacting the PP and of its severe austerity measures.
He argued that the Socialists must move further to the left to have any hope of winning back voters who have drifted to new far-left party Podemos, formed out of the anti-austerity protest movement known as the "Indignados" which occupied squares across Spain in 2011.
"The Socialist Party will carry out a useful opposition to defend people who are tired of the corruption of the PP and who suffer job insecurity and austerity," Sanchez told supporters after the results were confirmed.
"Tomorrow, everything starts. We are going to build a new PSOE to regenerate Spain."
Rajoy has relied on the Socialists - led by a caretaker administration since Sanchez was ousted -- to pass key measures in parliament since the premier was sworn in for a second term in October.
He has signalled that he won't put up with the opposition undermining his minority government and could be tempted to call new elections to try to secure a majority.
"I expect that within a year, a year and a half at the most, Rajoy will call early elections," a former PSOE strategist, who spoke on condition of anonymity, told AFP.
The third candidate in the race, Patxi Lopez, former president of the northern Basque Country, won 10 percent of the vote.
Much like Britain's Labour party leader Jeremy Corbyn, who survived a leadership challenge last year but still faces internal dissent, Sanchez enjoys considerable support among grassroots Socialist activists even though he is opposed by party heavyweights.
"Party members are with Pedro," Juan Jose Orts, a member since 1974, told AFP as he celebrated Sanchez's win outside PSOE headquarters in Madrid with dozens of other supporters.
"This is where he was killed and this is where he was resuscitated."
Ahead of Sanchez lies the mammoth task of bringing lustre back to a deeply divided party that has run out steam - much like fellow socialists in France and Britain - weakened by Podemos's rise.
The audiences at rallies for Sanchez and his rivals in the leadership race were mainly middle-aged or older, highlighting the party's struggle to appeal to younger voters.
The PSOE, which governed Spain until 2011, has lost ground in recent elections.
It won just 85 of the 350 seats in parliament in last year's general election, its worst showing since Spain returned to democracy following the death of longtime dictator Francisco Franco in 1975.
The party lost almost half of its voters to Podemos, which has made no secret of wanting to replace the PSOE as Spain's main left-wing force.
By Alvaro Villalobos and Daniel Silva / AFP