Mr. Sanders’ win is a blow to Ms. Clinton, the presumptive front-runner, just over a week after she eked out a hairsbreadth victory in the Iowa caucuses. Mr. Sanders’ triumph tonight is a coup symbolically, many pundits predicted it weeks in advance, as most polls have shown him leading her for roughly a month.

The leftist legislator cast his win as a victory over his favorite foil, the ultra-wealthy and their influence on the political system.

“Together, we have sent a message that will echo from Wall Street to Washington and from Maine to California that the government of this great nation belongs to all of the people and not just a handful of wealthy campaign contributors and their super PACs,” Mr. Sanders told his adoring supporters. “We served notice to the political and economic establishment of this country that the American people will not continue to accept a corrupt campaign finance system that is undermining American democracy and a rigged economy.”

The remarks also echoed his recent attacks on Ms. Clinton for accepting large donations and fees for private speeches from major financial firms. The candidates tussled bitterly in the most recent debate over which of them had the right to call themselves “progressive.”

Ms. Clinton won the first-in-the-nation primary in her 2008 campaign against Then-Sen. Barack Obama, but Mr. Sanders enjoyed the advantage of hailing from a neighboring state. The senator alluded to Ms. Clinton’s victory eight years ago, and to contest her repeated claim to be carrying on the president’s legacy.

“We were taking on the most powerful political organization in the United States of America, a team that defeated Barack Obama,” he said.

The New Hampshire primary results came eight days after another come-from-behind finish for Sanders in precinct caucuses in Iowa, where the one-time distant underdog fought Clinton to a virtual tie.

But Mr. Sanders, a self-described democratic socialist, will need plenty of momentum if he is to catch up with Ms. Clinton in South Carolina and Nevada, the next two primary states, where she enjoys a respective 30 and 20 point advantage. Much of her strength there, as in many of the Super Tuesday states, is staked on her popularity among minorities.

The Brooklyn-born lawmaker will have to expand his appeal beyond his white liberal base to compete as the contest continues.

Updated to reflect the most recent vote totals and comments by Mr. Sanders.