Democrats came out of a chaotic Iowa caucus hoping the New Hampshire primary would provide more clarity.

Instead, as results continued to trickle in late into the night, Sen. Bernie Sander maintained a razor thin lead over Pete Buttigieg, the former mayor of South Bend, Indiana, and was ultimately declared the winner by a margin of a few thousand votes out of more than 260,000 cast in the Democratic primary.

Beneath the top two candidates, the results seemed to reshape the Democratic presidential race.

Sen. Amy Klobuchar, who came in fifth in Iowa, suddenly caught fire over the past several days and grabbed third place in New Hampshire. The top three Democrats were projected to each receive six delegates from New Hampshire; no other candidate will get any.

Meanwhile, Massachusetts Sen. Elizabeth Warren and former Vice President Joe Biden were both left talking about how much time they still have to get their campaigns on track.

Sanders’ strength in New Hampshire was never in question, given that he crushed Hillary Clinton by 20 percentage points in the 2016 Democratic primary. But the results cemented Buttigieg among the Democratic front-runners as he was able to nearly match Sanders on the senator's home turf.

The results also capped a stunning rise for Klobuchar, who has never polled above 5 percent in any national Democratic survey. Many commentators said her strong performance in Friday’s Democratic debate drew new attention to her moderate message.

Republicans also held a primary in New Hampshire Tuesday, but President Donald Trump has no serious opposition for the nomination. The only question was whether the protest candidacy of former Massachusetts Gov. William Weld would make any meaningful dent in Trump’s vote tally. Trump won Iowa with 97 percent of the vote; in New Hampshire he took more than 85 percent of the vote.

With more than 75 percent of the precincts reporting, Buttigieg had about 24 percent of the vote, while Sanders had about 26 percent.

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

Buttigieg told his supporters during a late Tuesday speech in Nashua that, "in a state that goes by the motto live free or die, you made up your own minds. You inserted that famous independent streak and thanks to you a campaign that some have said shouldn’t be here at all has shown that we are here to stay."

Biden, who has led nearly every Democratic poll for more than year, announced earlier in the day he would hold a kick-off event Tuesday evening for his campaign in South Carolina. The South Carolina primary is on Feb. 29, and his campaign has told reporters that they expect the state to play a key role in his path to the Democratic nomination.

At a campaign event in South Carolina, Biden said, “It ain’t over man, we’re just getting started.”

Speaking to a heavily minority crowd, Biden said 99.9 percent of African Americans have not yet had a chance to vote in the Democratic primaries. ”You can’t be the Democratic nominee and you can't win the general election as a Democrat unless you have overwhelming support from black and brown voters,” he said.

Klobuchar stepped to a microphone at her victory party in Concord and announced, “Hello, America, I‘m Amy Klobuchar, and I will beat Donald Trump.”

The results will also launch soul-searching for Warren, who has raised more money than any other Democrat except Sanders, yet now heads into the Nevada caucus Feb. 22 desperately needing a strong showing. In a speech Tuesday evening, Warren vowed to fight on.

“We 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.”

The first casualty of the night was entrepreneur Andrew Yang, who announced he would be dropping out of the race after polling in the single digits for months and failing to crack even the top five in either of the first two contests. Colorado Sen. Michael Bennet dropped out a little later in the evening. Former Massachusetts Gov. Deval Patrick received less than 1 percent of the vote and said he was reconsidering whether to continue his campaign.

The New Hampshire primary follows the tumultuous Iowa caucuses last week, during which results were delayed due to problems with a new smartphone app the Iowa Democratic Party had released to collect caucus results more quickly.

Over the next several days, Democratic officials eventually announced that Buttigieg had come in first, winning 13 out of Iowa’s 41 available delegates. Sanders came in second, winning 12 delegates, Warren received eight delegates and Biden collected six. Both Buttigieg’s and Sanders’ campaigns filed requests Monday for a recanvass of select Iowa precincts.

With no reliable results out of Iowa, the Democratic field has been unusually fluid, and many voters were telling pollsters as late as Monday that they hadn’t made up their minds.

New Hampshire, generally considered a swing state, went for Hillary Clinton in the 2016 presidential election by the narrowest of margins. Clinton won the state with 46.8 percent of the vote, beating Donald Trump by 0.3 percentage points. Yet, Trump and his supporters are expecting him to win big in New Hampshire this year. At a rally Monday evening, Trump told the crowd at Southern New Hampshire University that the Democratic party is weak.

“They’re all fighting each other. They don’t know what they’re doing. They can’t even count their votes,” Trump said, referring to the Iowa caucus issues. “My only problem is I’m trying to figure out who is their weakest candidate. I think they’re all weak.”

Angela Fu is an intern with WGBH's New England Center for Investigative Reporting.