WILMINGTON, Del. — Joe Biden was elected the nation’s 46th president Saturday in a stinging repudiation of Donald J. Trump, a fiercely fought victory that sparked joy and pent-up celebration in a country reeling from economic hardship, the ravages of a deadly pandemic and deep-seated racial and political animosities.

The result came after four cliffhanging days of counting when Pennsylvania, the state where he was born, tipped Biden’s way. That made President Trump the first incumbent to lose a bid for reelection since George H.W. Bush in 1992.

It also ushered the nation to a historic milestone as California Sen. Kamala Harris became the first woman, first Black person and first Asian American to be elected vice president.

In an extraordinary break with custom, Trump declined to concede or congratulate Biden, vowing instead to continue his fight to overturn the election by pressing false claims of fraud and Democratic lawbreaking.

But Biden, sounding a gracious tone before a jubilant crowd in his hometown of Wilmington, Del., ignored the president and the snub, and addressed a portion of his remarks to Trump’s supporters. Having failed in two previous attempts at the White House, he said, he understood disappointment.

“Now let’s give each other a chance,” Biden suggested.

“It’s time to put away the harsh rhetoric,” he said, hollering into the crisp night air. “Lower the temperature, see each other again, listen to each other again.”

“Let this grim era of demonization in America begin to end here and now,” he went on, vowing to get to work on Monday by appointing a panel of medical and scientific experts to write the details of a plan to address the COVID-19 pandemic as soon as he takes office in January.

He called for compromise and a willingness by Democrats and Republicans to work together in Washington, echoing the words that launched Barack Obama’s national political career at the Democratic convention 16 years ago.

“I pledge to be a president that seeks not to divide, but unify. Who doesn’t see red states and blue states, only sees the United States,” Biden said.

Biden and Harris’ victory turned Saturday into an instant national holiday — at least for those of a certain political outlook — combining Thanksgiving, Christmas and the Fourth of July.

People danced in the streets of Washington, D.C., where a cheering swarm partied outside the White House, in the San Francisco Bay Area and in Democratic harbors throughout the nation. Revelers banged pots and pans from Los Angeles to Pittsburgh and Puerto Rico.

Honking and shouts echoed down New York City’s skyscraper canyons, where crowds cheered Postal Service trucks — a symbol of the mail ballots that helped torpedo Trump’s chance of a second term. Film director Spike Lee, wearing a face mask and a Yankees baseball cap, jumped up and down and popped a bottle of sparkling wine in the middle of the street.

Good wishes poured in from around the world, as leaders sent Biden and Harris their tidings, not waiting for Trump to concede.

“Congratulations! The American citizens have made their decision,” said German Chancellor Angela Merkel. “I am looking forward to working with President Biden in the future. Our transatlantic friendship is irreplaceable if we want to master the great challenges of our time.”

Indian President Narendra Modi, who had cultivated relations with Trump, sent a message congratulating Harris, whose mother was born in India.

The vice president-elect, who introduced Biden on Saturday night, spoke of her late mother’s journey as an immigrant to America and noted her own place as a trailblazer.

“While I may be the first woman in this office, I will not be the last,” she said, drawing a roar from the crowd. “Because every little girl watching tonight sees that this is a country of possibilities.”

Not all shared the jubilation. Counter-demostrations quickly sprang up.

In Lansing, Mich., several dozen supporters of the president stood on the steps of the state Capitol — maskless, shoulder to shoulder — chanting, “Four more years!” and “Count legal votes!”

“These Democrats are totally trying to steal this election,” said a woman, Rochelle, who would give only her first name.

Trump fed the conspiracy theories.

“Beginning Monday, our campaign will start prosecuting our case in court to ensure election laws are fully upheld and the rightful winner is seated,” Trump said in a statement after the Associated Press and television networks called the race for Biden.

So far, however, Trump’s efforts in court have made little progress, with judges in several states dismissing his legal claims for lack of evidence.

Few prominent Republicans rallied to the president’s side; some, including Utah Sen. Mitt Romney, the 2012 Republican presidential nominee, congratulated Biden and Harris.

“We know both of them as people of good will and admirable character,” Romney wrote in a message from him and his wife, Ann. “We pray that God may bless them in the days and years ahead.”

Trump was undeterred. Heading to his Virginia golf course Saturday morning he tweeted, “I WON THIS ELECTION, BY A LOT!” He tweeted a similar message in the afternoon after returning to the White House.

Biden’s victory took several days to emerge, but the contest, in the end, was not close.

If the Democrat holds onto his current lead he will have the same tally of electoral votes, 306, as Trump in 2016. Unlike the president, Biden also won the popular vote.

Amid a record turnout, the former vice president’s performance — 75 million votes and counting — puts him on track for a 4% to 5% margin of victory, significantly bigger than average in presidential contests in the past generation.

It is not often a sitting president loses reelection. But Trump faced powerful countervailing forces: a once-in-a-century pandemic, the resulting economic collapse, and a wrenching debate over the country’s painful history of racial discrimination.

Alone among modern presidents, Trump never surpassed an approval rating of 50% in a reliable opinion survey. His provocative behavior, racist comments and trampling of norms ensured that.

But he also never tried to broaden his support, focusing on his base among conservative, mostly white, rural and exurban voters — many of them thrilled by his outrageous antics — while largely ignoring, or antagonizing, others.

Trump’s cavalier handling of the COVID-19 pandemic — which has killed more than 237,000 Americans and sent millions more to the hospital — proved his undoing in the way the Vietnam War ended the career of President Johnson, the Iranian hostage crisis damaged President Carter, and a limp economy hurt Bush.

All either lost or gave up their reelection hopes in great part because they appeared to be overmatched by events.

Even so, Biden’s victory was hard-won and came only after a days-long and divisive counting of ballots, brought on by fears of the pandemic, which led many to vote by mail.

Contributing to the delay were the machinations of Trump and his supporters, who blocked measures in several states that would have sped up the tabulations.

The final drama rested on four states — Pennsylvania, Georgia, Nevada and Arizona — where vote-counting continued despite Trump’s efforts to thwart election officials. The turning point came Friday, when Biden pulled ahead in Pennsylvania and Georgia, either of which offered enough electoral votes to win the White House.

On Saturday, the AP and television networks projected Biden the winner in Pennsylvania after his lead expanded beyond half a percentage point, the state’s threshold for a mandatory recount.

Nevada and its six electoral votes tipped into Biden’s column soon after. Georgia, where the margin is much closer, is headed for a recount, and in Arizona, the tabulations continue with Biden narrowly ahead.

The Trump campaign has also demanded a recount in Wisconsin, and may seek others. Historically, however, such canvasses seldom change the outcome by more than a few hundred votes. Biden’s margin in even the closest states is several times larger.

His victory marked one of the most remarkable political comebacks in recent history, after the Democrat was nearly eliminated in the party’s primaries.

Biden, who turns 78 on Nov. 20, is the oldest president-elect in history. (He trotted down a runway to take the stage Saturday night, as if to show his vigor.)

His four decades in public life began under Richard Nixon’s presidency, when Biden was first elected to the Senate from Delaware, and includes eight years as vice president under Obama.

From the outset of his campaign, Biden cast the election as a “battle for the soul” of a country — words he used again Saturday night — that was being transformed for the worse by an erratic and divisive Trump. He repeatedly hammered the incumbent for his handling of the pandemic, framing it as part of a larger failure of leadership that split the country and diminished America abroad.

First, though, Biden had to survive a fight for the nomination.

He prevailed over a sprawling field of mostly younger and more progressive rivals largely because Democrats viewed him as likeliest to defeat Trump, unify the party and win back white working-class voters who had defected to Trump.

Reclaiming Pennsylvania, Wisconsin and Michigan — traditionally Democratic states in the Industrial Belt that Trump carried in 2016 — validated that assessment.

Biden also won independents, the free-floating bloc that often decides elections, 54% to 40%, according to preliminary exit polling. Four years ago, Trump won among independent voters.

Although some fellow Democrats criticized Biden as too old, too centrist and too politically cautious, he outperformed the 2016 nominee, Hillary Clinton, by several measures in addition to winning a larger share of the popular vote. He cut Trump’s margin among male voters, nearly breaking even, and outperformed Clinton among female voters even as she ran to become the first woman to win the White House.

With the COVID-19 pandemic as a worrisome backdrop, Biden carried voters older than 65, a group that went for Trump four years ago. Biden also expanded Democratic support among voters ages 18 to 29, who strongly preferred his left-leaning rival, Vermont Sen. Bernie Sanders, in the primaries.

For a time it seemed Biden’s presidential candidacy would end in the same failure as his previous tries in 1988 and 2008.

He finished a dismal fourth in the Iowa caucuses, and eight days later performed even worse in the New Hampshire primary, finishing fifth. By then, many had written him off. Biden insisted, however, that those first two contests in small, heavily rural states were not representative of the country, or more particularly the base of the Democratic Party, and that he would fare better in the next set of contests.

He was right.

Biden finished second in the caucuses in Nevada, which has a far more diverse electorate than Iowa or New Hampshire. That vaulted him into the South Carolina primary three days later. His endorsement there by Rep. James E. Clyburn, the most powerful Black lawmaker in Congress, proved vital in a state where about 6 in 10 Democratic primary voters were Black.

He won the state in a landslide and then rapidly reeled off a string of victories as Democrats rallied around him. By mid-March, the matchup with Trump was set, and Biden was on a steady course to victory and the White House.

Barabak reported from San Francisco, Hook from Washington and Bierman from Wilmington. Times staff writers Michael Finnegan in Philadelphia, Kurtis Lee in Lansing, Mich., and Chris Megerian in Washington contributed to this report.