Hamon won with 58.9 percent of the vote, according to the Socialist Party website, defeating Valls who had 41.1 percent.
So far, 76.61 percent of the votes have been counted. The Socialist Party website states that more than 1.7 million people voted in the primary election.
Hamon's win defied polls that had predicted a runoff between Valls and former economy minister Arnaud Montbourg.
Hamon thanked those who voted for him in a tweet, saying "I warmly thank the left-wing voters for giving me, by their vote, a considerable strength for the fights to come."
Valls issued a tweet of his own: "Serving France for five years has been an honor, I will never forget it."
The first round of the presidential election will be held on April 23 and the second on May 7, with legislative elections due to be held immediately afterward in June.
But the challenge may only just have started for Hamon, who now faces the difficult task of reuniting a bitterly divided party decimated even further by a fractious primary campaign.
The vote, the second primary ever held by the Socialist Party, was seen as a contest between its two opposing wings.
On the left, Hamon, who campaigned on a platform which included the creation of a universal basic income, the legalization of cannabis and a so-called "robot tax," which would be applied on technology that takes away jobs from humans
To the right, Valls, hampered by being forced to defend not only the government's record but also his own.
Paying the price?
Twice while prime minister, Valls used an unpopular constitutional measure that allowed him to bypass the opposition from within his own party in order to push controversial reform measures through.
Such moves did not enamor him to colleagues, who appeared reluctant to forgive him, as highlighted by the television debates in which he often appeared adrift and isolated.
The debates between the seven candidates also exposed the deep fault lines running through France's socialist party, with the result a measure of the discontent within the party's rank and file at Francois Hollande's government.
Hollande's chose not to run for re-election
, which paved the way for Valls' candidacy, but the former prime minister appears to have paid the cost of the government's low ratings.
Shift in support?
On the other side of the contest, opinion polls suggest that Republican candidate Francois Fillon is losing some of his advantage over the far-right's Marine Le Pen.
The allegations that Fillon paid his wife around half a million euros of parliamentary funds for a non-existent job as his parliamentary secretary threaten to overshadow his campaign.
The polls put Emmanuel Macron in third place and have consistently predicted a distant fifth-place finish for the socialists behind the far-left's Jean-luc Melenchon.
But once again, the socialist primary has shown the unreliability of polls.
As the seven candidates went into the first round, a substantial victory was predicted for Valls, with the runner-up expected to be Montebourg.
The polls were also wrong at the time of the Republican primary in November.
Before the first round, polls were predicting a Nicolas Sarkozy-Alain Juppé runoff with Fillon in third place.