MANCHESTER, N.H. — Bernie Sanders won New Hampshire’s presidential primary election Tuesday night, narrowly edging moderate rival Pete Buttigieg and scoring the first clear victory in the Democratic Party’s chaotic 2020 nomination fight.

In his win, the 78-year-old Sanders, a self-described democratic socialist, beat back a strong challenge from the 38-year-old former Midwestern mayor — two men representing different generations and wings of their party.

“This victory here is the beginning of the end for Donald Trump,” Sanders declared.

As Sanders and Buttigieg celebrated, an unexpectedly strong performance from Amy Klobuchar gave her a path out of New Hampshire as the contest moves on to the string of state-by-state primary contests that lie ahead. Joe Biden and Elizabeth Warren posted disappointing results and were on track to finish with zero delegates from the state.

The New Hampshire vote made clear that the early days of the Democratic contest will be a battle largely between two men four decades apart in age and ideological opposites. Sanders is a leading progressive voice, calling for substantial government intervention in health care and other sectors of the economy. Buttigieg has pressed for more incremental changes, giving Americans the option of retaining their private health insurance and making a point of appealing to Republicans and independents who may be dissatisfied with Trump.

“Thanks to you, a campaign that some said shouldn’t be here at all has shown that we are here to stay,” Buttigieg told cheering supporters.

Yet Sanders and Buttigieg enter the next phase of the campaign in different political positions.

While Warren made clear she will remain in the race, Sanders, well-financed and with an ardent army of supporters, is quickly becoming the leader of the progressive wing of the party.

Meanwhile, Buttigieg still has moderate rivals to contend with, including Klobuchar, whose standout debate performance led to a late surge in New Hampshire. Biden promises strength in upcoming South Carolina, while former New York Mayor Mike Bloomberg was not on Tuesday’s ballot but looms next month.

After a chaotic beginning to primary voting last week in Iowa, Democrats hoped New Hampshire would provide clarity in their urgent quest to pick someone to take on Trump in November. At least two candidates dropped out in the wake of weak finishes Tuesday night: just-the-facts moderate Colorado Sen. Michael Bennet and political newcomer Andrew Yang, who attracted a small but loyal following over the past year and was one of just three candidates of color left in the race.

While struggling candidates sought to minimize the latest results, history suggests that the first-in-the-nation primary will have enormous influence shaping the 2020 race. In the modern era, no Democrat has ever become the party’s general election nominee without finishing first or second in New Hampshire.

The action was on the Democratic side, but Trump easily won New Hampshire’s Republican primary. He was facing token opposition from former Massachusetts Gov. Bill Weld.

Warren, among the front-runners for months, offered an optimistic outlook as she faced cheering supporters: “Our campaign is built for the long haul, and we are just getting started.”

Having already predicted he would “take a hit” in New Hampshire after a distant fourth-place finish in Iowa, Biden essentially ceded the state. He traveled to South Carolina Tuesday as he bet his candidacy on a strong showing there later this month boosted by support from black voters.

He was not on the ballot, but Bloomberg, a New York billionaire, loomed over the New Hampshire contest as moderates failed to embrace a single alternative to Sanders. The former New York City mayor was skipping all four states that vote this month in favor of dozens of delegate-rich contests in March. He’s already devoted hundreds of millions of dollars to the effort.

Bloomberg returns to the campaign trail Wednesday with stops in Tennessee but will face fresh scrutiny after audio emerged on Tuesday raising new questions about his support for New York’s controversial stop-and-frisk policy found to disproportionately target people of color.

Sanders and Buttigieg were on track to win the same number of New Hampshire delegates with most of the vote tallied, and Klobuchar a few behind. Warren, Biden and the rest of the field were shut out, failing to reach the 15% threshold needed for delegates.

AP has allocated six delegates each to Sanders and Buttigieg and four to Klobuchar with eight still to be called.

After Iowa and New Hampshire, Buttigieg held a one delegate lead over Sanders with one Iowa delegate yet to be allocated and Buttigieg leading ever so slightly in that contested count. Klobuchar now joins a close knot in a second tier with Warren and Biden, about ten delegates behind the two leaders.

Democrats were closely monitoring how many people showed up for Tuesday’s contest. New Hampshire’s secretary of state predicated record-high turnout, but if that failed to materialize, Democrats would confront the prospect of waning enthusiasm following a relatively weak showing in Iowa last week and Trump’s rising poll numbers.

The political spotlight quickly shifts to Nevada, where Democrats will hold caucuses on Feb. 22. But several candidates, including Warren and Sanders, plan to visit states in the coming days that vote on Super Tuesday, signaling they are in the race for the long haul.

Peoples reported from Washington. Associated Press writers Seth Borenstein in Washington, Will Weissert, Holly Ramer and Thomas Beaumont contributed from New Hampshire.