The left-wing senator took a tight victory over centrist former mayor Pete Buttigieg, who offered a different Democratic vision in the race to take on President Trump in November.
Mr Sanders declared the night "the beginning of the end" of Mr Trump.
Success in New Hampshire - like Iowa - offers momentum for the race ahead.
Finishing behind Vermont senator Mr Sanders were two moderates - Mr Buttigieg and Minnesota senator Amy Klobuchar, who emerged as a surprise contender by taking third place.
Massachusetts senator Elizabeth Warren and Mr Biden - two erstwhile frontrunners - finished in fourth and fifth places.
Technology entrepreneur Andrew Yang and Colorado senator Michael Bennet both dropped out of the race.
Although President Trump faces a long-shot challenger in the Republican primaries he is almost certain to win the nomination because he enjoys near-record support in his party.
What were the results in New Hampshire?
Some 280,000 Democratic voters cast ballots in the Granite State on Tuesday night, delivering 26% to Mr Sanders.
With more than 90% of the vote counted, Mr Sanders, 78, led Mr Buttigieg, the 38-year-old the former mayor of South Bend, Indiana, by only 1.6%, or about 4,300 votes.
Mr Sanders hailed a "great victory" as he thanked supporters from a rally in Manchester, New Hampshire.
"This victory here is the beginning of the end for Donald Trump," he said, and promised to build an "unprecedented multi-generational, multi-racial political movement" to defeat the Republican.
The result will give Mr Sanders nine of the 24 delegates who will represent New Hampshire at the July Democratic national convention, where the party crowns a nominee based on the delegates won.
Although he had fewer votes, Mr Buttigieg will also get nine delegates. Thanking supporters, he warned against succumbing to "a polarised vision" of politics and pitched himself as the centrist to bring new voters into the party.
Unlike in Iowa last week, where turnout was disappointing, New Hampshire voters turned out in higher numbers than in 2016.
Could Sanders go all the way?
Joe Biden - the only candidate Bernie Sanders trails in national polling - is wounded, perhaps mortally so.
Pete Buttigieg finished a strong second, but his success outside the first two states is still an open question. Warren, his closest rival for the liberal left vote, has yet to prove she can finish near Sanders. Amy Klobuchar's success ensures she'll stick around and the moderate support will remain splintered.
In 2016 Sanders hit an electoral brick wall after New Hampshire. With plenty of money, a battle-tested national campaign organisation and divided opposition, his path ahead - while far from certain - looks the brightest of any in the field.
He would be the most left-wing candidate the party has nominated since George McGovern, however, and there are plenty of establishment Democrats old enough to have heart palpitations remembering the 1972 drubbing he took at the hands of Richard Nixon.
What had happened last week in the first contest?
Mr Buttigieg, the young former mayor, led the delegate count going into New Hampshire after winning the Iowa caucuses.
Results there were delayed due to technical problems and the party, as well as several candidates, have demanded a re-tallying of the votes.
Unlike Iowa, New Hampshire's contest is a traditional primary where voters cast secret ballots that are counted by the state, not the party.
Both states are largely rural and white, and not representative of the larger Democratic electorate.
A question remains over whether the frontrunners who have emerged from Iowa and New Hampshire can attract a broader coalition of voters that includes voters of colour.
Mr Sanders has polled as high as 19% among black voters, but in some polls, Mr Buttigieg has less than 10%.
What were the other stories from New Hampshire?
Ms Klobuchar, 59, posted a better than expected finish following a strong debate performance last week.
The Minnesota senator campaigned energetically in the last few days before the vote, adding stops in three cities between Sunday and Tuesday.
Though her third place finish meant she would only win six delegates, the result invigorated her campaign, giving her momentum to go on to Nevada and South Carolina, the next two states to hold contests.
"Everyone had counted us out even a week ago," Ms Klobuchar said. "I came back and we delivered".
A casualty of the night was Mr Yang, a political newcomer. His quixotic run centred on the promise of a universal basic income and addressing the challenges of automation inspired an enthusiastic, but ultimately marginal, following.
Mr Yang said he was proud of a campaign that had "brought a message of humanity first and a vision of an economy and a society that works".
Mr Bennet, the Colorado senator, also suspended his campaign. He had not qualified for a debate since August 2019 and had been polling at less than one percent nationally.
Ms Warren, 70, received no delegates, but said she would continue. "Our campaign is built for the long haul and we are just getting started," she said.
Is it a last stand for Biden?
For months after he entered the race in April 2019, Mr Biden was seen as the frontrunner, polling as high as 41% nationally.
The former vice-president, 77, finished fourth in Iowa, and his support has dropped to 20% nationally, putting him behind Mr Sanders for the first time.
In a sign that he anticipated another disappointing finish on Tuesday, Mr Biden departed New Hampshire before the last polls had closed to head to South Carolina.
Support for Mr Biden is stronger among African-American voters, who make up some two-thirds of the southern state's Democratic primary electorate.
However, a Quinnipiac poll showed that the support dropped after the Iowa caucuses, from 52% to 27% - though, Mr Biden is still the most popular Democrat for black voters.
What's next in the race?
The race moves next to the Nevada caucuses on 22 February.
The candidates will look to post strong performances there and in South Carolina, to show that they can appeal to a more diverse electorate.
Then, on Super Tuesday, 3 March, 15 states and territories - with 1,344 delegates - will vote.
The field will be whittled down until a final nominee is crowned at the party convention in Milwaukee, Wisconsin, in July.