MANCHESTER, N.H. — Sen. Bernie Sanders narrowly won the New Hampshire Democratic primary by about 4,000 votes, or less than 2 percentage points, over Pete Buttigieg, according to an NBC News projection.

Sanders, I-Vt., had been leading in the polls, so his victory wasn't a surprise. But he and Buttigieg, the former mayor of South Bend, Indiana, were closely bunched with the third-place candidate, Sen. Amy Klobuchar, D-Minn., allowing all three to claim either victory or solid momentum going into the next round of voting.

At the same time, former Vice President Joe Biden and Sen. Elizabeth Warren, D-Mass., were headed toward poor showings and failed to get any delegates, NBC News projected.

Download the NBC News app for breaking news and politics

With 95 percent of precincts reported, Sanders had 26 percent, or 71,950 votes; Buttigieg was at 24.4 percent, or 67,577 votes; Klobuchar had 19.7 percent, or 54,609 votes; Warren had 9.3 percent, or 25,765 votes; and Biden was at 8.4 percent, or 23,205 votes, according to the NBC News tally.

Candidates must meet a threshold of 15 percent in the state's two congressional districts or statewide to win delegates. Aware of a poor showing, Biden had already left New Hampshire for South Carolina, the site of the next primary.

Sanders, Klobuchar and Buttigieg had all reached the threshold to win delegates at the congressional district and statewide level, according to NBC News. With 24 delegates at stake, Sanders and Buttigieg each had nine, and Klobuchar won six.

Addressing a raucous crowd at his election night event, Sanders pledged to win in Nevada and South Carolina, too.

"And what I can tell you, with absolute certainty, and I know I speak for every one of the Democratic candidates, is that no matter who wins — and we certainly hope it's going to be us — we're going to unite together," he said. "We are going to unite together and defeat the most dangerous president in the modern history of this country.

"The reason that we are going to win is that we are putting together an unprecedented, multigenerational, multiracial political movement," he added. "And this is a movement from coast to coast, which is demanding that we finally have an economy and a government that works for all of us, not wealthy campaign contributors."

At his election night event, Buttigieg said he "admired" Sanders and added that he respected Sanders when he was a high school student and "I respect him greatly to this day, and I congratulate him on his strong showing tonight."

But Buttigieg appeared to make some comments critical of Sanders' platform.

"Americans want the freedom to make choices for themselves on health care or on any other issue, not to have Washington decide for them, and a politics of 'my way or the highway' is a road to re-electing Donald Trump," Buttigieg said. "Vulnerable Americans do not have the luxury of pursuing ideological purity over an inclusive victory."

When Buttigieg's speech began projecting at Sanders' election night event, Sanders' supporters began loudly booing and chanting "Wall Street Pete," "Bernie beats Trump" and "popular vote."

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

NBC News exit polls showed that 48 percent of the state's Democratic primary voters made up their minds within the past few days — a substantial increase from 25 percent who said they did in 2016 and 38 percent in 2008. The same proportion of Democratic voters, 48 percent, said Friday's debate, in which Klobuchar's performance was widely praised, was either the most important or an important factor in their choice.

At her primary night event, Klobuchar said her campaign has "beaten the odds every step of the way."

"We have done it on the merits," she said. "We have done it with ideas. And we have done it with hard work, because we are resilient and strong as the people of this great nation."

"If you want a nominee who can stand up to Donald Trump on that debate stage, which you well know I can do, I need your votes, yes," she said, adding: "I don't have that big bank account. I don't have that big name as some of the other people that are in this race. And I am not a newcomer with no political record. But what I do is get things done. What I have is your back."

Addressing supporters in Manchester, Warren said Democrats "might be headed for another one of those long primary fights that lasts for months."

"We're two states in, with 55 states and territories to go," she said. "We still have 98 percent of the delegates for our nomination up for grabs, and Americans in every part of our country are going to make their voices heard."

She added that Sanders and Buttigieg are "great people" and would be better than Trump but that "the fight between factions in our party has taken a sharp turn in recent weeks, with ads mocking other candidates and with supporters of some candidates shouting curses about other Democratic candidates."

"These harsh tactics might work if you are willing to burn down the rest of the party in order to be the last man standing," she said. "They might work if you don't worry about leaving our party and our politics worse off than how you found it, and they might work if you think only you have all the answers and only you are the solution to all our problems."

Biden pledged to return to New Hampshire in the fall "to defeat Donald Trump in November."

"So don't go away. You're not getting rid of us. We're coming back, and we love you," he said from South Carolina.

Speaking to supporters in South Carolina, Biden said: "We just heard from the first two of 50 states, not all the nation, not half the nation, not a quarter of the nation, not 10 percent, two, two."

"Where I come from, that's the opening bell," he added. "Not the closing bell, and the fight to end Donald Trump's presidency is just beginning, just beginning."

On the Republican side, Trump won the GOP primary, NBC News projected. The president spent much of Tuesday night jabbing Democrats on Twitter, saying, "A lot of Democrat dropouts tonight, very low political I.Q."

He tweeted at Warren, writing that she was "having a really bad night" and adding that he thinks "she is sending signals that she wants out." Trump also took aim at former New York Mayor Michael Bloomberg, who is not participating in the early primary states, saying he was having "a very bad night."

Of Buttigieg, Trump wrote: "Bootedgeedge (Buttigieg) is doing pretty well tonight.

"Giving Crazy Bernie a run for his money," he continued. "Very interesting!"

Meanwhile, upstart Democratic presidential candidate Andrew Yang dropped out of the race Tuesday evening, sources close to the campaign said. Yang rose from obscurity to become a second-tier player in the primary and outpaced a number of prominent current and former elected officials.

"We have touched and improved millions of lives and moved this country we love so much in the right direction," Yang told supporters in Manchester. "And while there is great work left to be done, you know I am the math guy, and it is clear tonight from the numbers that we are not going to win this race. I am not someone who wants to accept donations and support in a race that we will not win. And so tonight I am announcing I am suspending my campaign for president."

In addition, Sen. Michael Bennet, D-Colo., announced that he, too, was dropping out of the race following a poor showing in New Hampshire.

"I wish all those candidates well that are going beyond New Hampshire," Bennet told supporters. "I think it's fitting for us to end the campaign tonight, but I want to remind you of why I got in this race, why I stayed in this race and why we have to stay in this fight."

Entering primary day, Sanders was comfortably leading in the polls, trailed by Buttigieg. Behind them was a three-way battle for third place among a surging Klobuchar, Warren and Biden.

The vote came as last week's Iowa caucus results were being contested. Both Sanders and Buttigieg have claimed victory there.

Democratic candidates spent the day of New Hampshire's first-in-the-nation primary talking up their candidacies and taking aim at a rival who wasn't even on the ballot here — Bloomberg, who is focusing on the March 3 Super Tuesday contests.

The billionaire surged in national polls as the New Hampshire vote neared, and he has poured more than $100 million into advertising so far, building up a large ground game in more than 30 states, too.

"This is what I think, you know, Mike Bloomberg and anybody else has every right in the world to run for president of the United States," Sanders said in an interview with NBC News anchor Lester Holt. "But I got a real problem with multibillionaires literally buying elections."

The early story of the day was Biden's decision to leave New Hampshire hours before the polls closed and head straight to South Carolina, where he enjoys an advantage with black voters, although that edge has been quickly narrowing.

"Look, the rest of the nation is out there," Biden said Tuesday at a Dunkin' store in Manchester. "There's an awful lot of electoral votes to be had, and we're going to see, and I think we are going to do well in Nevada and in South Carolina, and we'll go from there. We'll see."