With just over one-third of polling stations reporting, Hamon scored 35.21 percent to 31.56 percent for Valls, with former economy minister Arnaud Montebourg eliminated along with four other candidates, party electoral official Thomas Clay announced.
Hamon, 49, was not considered a serious contender when the campaign began but he beat Valls in the first round of the Socialist primary in a contest billed as a fight for the party's soul as Europe moves to the right.
Shortly after accepting defeat Montebourg, a former Economy Minister called on voters to back Hamon, who will now be the favorite to win the runoff next Sunday.
The surprise result naturally went down well with Benoit Hamon's supporters. The video below shows the outburst of joy when the initial result was announced.
Annonce des résultats chez Benoît Hamon. pic.twitter.com/EBDUWiBZKM
— Alex Sulzer (@Alexsulzer) January 22, 2017
Speaking after the results were announced ex-Prime Minister Manuel Valls told supporters and the media that everything was to play for in the second round. “Nothing is written,” he said.
He told voters they now faced a choice between the “unrealistic programme” offered by Hamon and the realistic chance of presidential victory.
Turnout in the Socialist primary was around half of that in the centre-right Republicans primary in November.
Between 1.7 million and 1.9 million voted, according to an estimate by the Elabe polling group as polling stations closed, compared to four million who took part in the first round of the rightwing primary.
The winner of next week's runoff vote faces a tough task to gain the keys to the Elysée Palace with the National Front's Marine Le Pen and the centre right candidate François Fillon the two frontrunners according to opinion polls.
Former Economy Minister in Manuel Valls' government Emmanuel Macron is also on the march as polls suggest he would also come ahead of any Socialist party candidate.
But many observers believe he has run a lacklustre campaign and former education minister Benoit Hamon — is now the favorite for next Sunday's runoff.
Hamon has performed strongly in the three TV debates of a short campaign, attracting attention with his proposal to pay the poor and 18 to 25-year-olds a “universal income” rising from 600 euros to 750 euros ($640 to $800) a month — an idea dismissed as ruinously expensive by Montebourg.
“I voted for Benoit Hamon because to me he is the one best placed to revive the Socialist party,” said Jean Claude, who cast his ballot in Millau, southwestern France.
Dominique, a man in his 40s who voted in eastern Paris, said he was backing Valls.
“My main concern is that the left reaches the second round (of the presidential election). Valls is the most credible option against Macron,” he said.