Hamon achieved 58.6 percent of the vote compared to 41.3 percent for the former Prime Minister Valls.
Hamon told jubilant supporters that the Socialist party had “turned towards the future”.
“I believe that faced with a conservative right that represents privilege and a destructive extreme-right our country needs a left that thinks of the world as it is and not as it was, a left that can bring a future people want.”
The pair later appeared for a symbolic photo opportunity in which they shook hands and waved to Socialist party members.
“Benoit Hamon won decisively,” Valls said in a concession speech.
“Benoit Hamon is henceforth the candidate of our political family,” added the former prime minister.
Hamon wooed voters with his staunchly leftist proposals, notably the idea of a universal basic income, dismissed as a “mirage” by Valls.
He also wants to legalise marijuana and tax robots that replace workers.
He has been referred to as the “French Jeremy Corbyn” or the “French Bernie Sanders”.
The 49-year-old Hamon, round-faced with a schoolboy haircut, has a crowd-pleasing eagerness that contrasts with the square-jawed assertiveness of Valls, 54, who was interior minister before becoming premier.
Hamon joined a rebellion against what he saw as the Socialist government's rightward drift under Valls and President Francois Hollande, quitting as education minister in 2014.
The combative Valls has dismissed him as a dreamer with no hope of becoming president in May.
But Socialist voters disagreed in big numbers.
Hamon's victory is another upset in an election seen as highly unpredictable, with the 49-year-old former education minister viewed as an outsider only three weeks ago.
His nomination completes the line-up of the main candidates in the two-round election in April and May which pollsters forecast will confirm France's shift to the right after five years of unpopular Socialist rule.
The candidates include rightwing Republicans party frontrunner Francois Fillon, far-right candidate Marine Le Pen, as well as centrist Emmanuel Macron who some analysts see as likely to benefit from the Socialists' tilt left.
Fillon, who was also a long-shot until he clinched the Republicans nomination in November, has consistently been tipped to become France's next leader.
But his campaign has been in turmoil since last Wednesday when a newspaper reported his wife had been paid around 500,000 euros ($540,000) over eight years for a suspected fake job as a parliamentary aide.
Those allegations have sparked a preliminary judicial enquiry, but there was more bad news for Fillon on Sunday.
Investigative website Mediapart and the Journal du Dimanche newspaper reported Fillon had used his parliamentary allowance to pocket up to 25,000 euros while working as a senator.