ATLANTA — Former labor secretary Thomas Perez was elected the first Latino chair of the Democratic National Committee on Saturday, narrowly defeating Rep. Keith Ellison (D-Minn.) at the end of a contentious battle over the fate of the beleaguered party in the age of President Trump.

Perez’s victory concluded the first contested race for the DNC leadership since 1985, a contest the party had extended by a month to allow more debate. It put in place the Democratic leadership that will navigate thousands of state and local elections — where the party hopes to reverse the losses of the past six years — and a 2020 presidential race that could divide the party again.

Ellison’s defeat was a blow to the party’s liberal wing, personified by activists, labor leaders and organizers, many of whom had supported the presidential bid of Sen. Bernie Sanders (I-Vt.) and had come to Atlanta to cheer Ellison on. Many of them warned that by picking Perez, the party was alienating the growing “resistance” that has organized against Trump.

The race was close enough that it required a second round of balloting, with Perez winning 235 of 435 votes cast. With tensions still high as the result was announced, nine Ellison supporters chanted “Party for the people, not big money!” and stormed out of the room.

“Someday, they’re going to study this era of American history,” Perez said after his win. “They’re going to ask the question of all of us: Where were you in 2017 when we had the worst president in the history of the United States? We will be able to say that the Democratic Party led the resistance and made sure this was a one-term president.”

Onstage, Perez gave Ellison the symbolic role of deputy party chair, and the Minnesota congressman gave a short speech asking his supporters to stay with the party and avoid recriminations.

“We don’t have the luxury to walk out of this room divided,” Ellison said.

Hillary Clinton, the 2016 Democratic presidential nominee, tweeted her support for both ­Perez and Ellison as representatives of a “unified party,” while former president Barack Obama congratulated “my friend” Perez in a statement.

“I know that Tom Perez will unite us under that banner of opportunity, and lay the groundwork for a new generation of Democratic leadership for this big, bold, inclusive, dynamic America we love so much,” Obama said.

Sanders, who had supported Ellison, said in a statement that it was “imperative that Tom understands that the same-old, same-old is not working and that we must open the doors of the party to working people and young people in a way that has never been done before.”

Trump, in classic fashion, responded to the election by simultaneously congratulating and belittling Perez in a tweet: “I could not be happier for him, or for the Republican Party!”

The vote itself was tense. On Friday night, Democrats gathered at a downtown Westin hotel here in Atlanta to meet, drink and lobby for votes, and the Ellison campaign — along with allies of South Bend, Ind., Mayor Pete Buttigieg, a third candidate — battled rumors that Perez might already have locked up the votes he needed.

But by Saturday morning, it was clear that the race was up for grabs. Buttigieg used his nomination speech to quit the race, endorsing no candidate. As most of the 439 DNC members present cast their votes — eight eligible members did not attend — several DNC members got a text from the Ellison camp saying the congressman was “grateful to have the support of Mayor Buttigieg,” an endorsement that the mayor denied.

In the first round of balloting, Perez won 213.5 votes to 200 for Ellison, 12 for Idaho Democratic Party Executive Director Sally Boynton Brown, 0.5 for Democratic strategist Jehmu Greene and one for Buttigieg in the first round of balloting. Greene endorsed Perez, while two fringe candidates who had won no votes backed Ellison. Members who are abroad get half a vote.

Perez’s victory did not represent a Democratic shift to the right. On key issues, Perez’s platform mostly resembled Ellison’s. Perez promised to refocus on small donors and online fundraising; Ellison set a goal for “low-dollar contributions from everyday Americans [to] account for 33 percent of revenue.” Ellison called for an “Innovation Hub” in Silicon Valley; Perez promoted DNC fellowships to “encourage developers, programmers, data scientists, [and] engineers.”

While Perez and Ellison praised each other personally, the race was defined for outsiders by Sanders’s support of the Minnesota lawmaker. Ellison was one of the few members of Congress who had backed Sanders for president. He billed himself as the “unity candidate” who would keep Sanders’s restive supporters in the party while embracing those who had backed Clinton.

In the first weeks after Ellison declared his candidacy, the strategy seemed to be working, despite some hiccups. Labor unions that had endorsed Clinton, such as the American Federation of Teachers and the American Federation of State, County and Municipal Employees, got behind Ellison.

Howard Dean, the most successful DNC chair in modern party history, dropped his plans to run again when Ellison said he would resign from Congress if elected to the full-time job. Senate Minority Leader Charles E. Schumer (D-N.Y.), who had frequently clashed with Dean over strategy and investments, endorsed Ellison and defended the first Muslim member of Congress against charges of anti-Semitism.

But veterans of the Obama administration, where Perez had been a popular liberal force, encouraged the former labor secretary to run — and starting Dec. 15, he did. In progressive media, the race was frequently covered as a clash between “the establishment” and the “revolution” that had been proved right by the 2016 election.

That was not how most DNC members chose to see it. In public forums, including the final one broadcast on CNN, Ellison and Perez declined to criticize each other. While progressive media outlets accused Perez of protecting the party’s consultant class, DNC members who broke for Perez said that he had convinced them that he knew what state parties needed.

“Tom seemed to have a better handle on the job,” said Kathy Sullivan, a former chair of New Hampshire’s Democratic Party, who endorsed Perez after current New Hampshire state chair Ray Buckley quit the race.

Perez was also helped by a string of endorsements from Obama administration veterans — although, as Ellison backers noticed, he did not win any high-profile supporters of Sanders’s to compete with Ellison’s endorsements from Clintonites. The Feb. 1 endorsement of Perez by former vice president Joe Biden, one of the party’s most beloved figures, prompted Sanders to criticize Perez for the first time.

“Do we stay with a failed status-quo approach or do we go forward with a fundamental restructuring of the Democratic Party?” Sanders asked in a statement after Biden’s endorsement. “I say we go forward and create a grassroots party which speaks for working people and is prepared to stand up to the top one percent.”

Most of the DNC’s membership — just 39 of whom had backed Sanders for president in 2016 — did not view the contest as a stark ideological clash. Sanders supporters, including Ellison, had largely succeeded in moving the party’s platform to the left. In interviews, some acknowledged that there would be walkouts by Sanders die-hards in their states, but they argued that the daily outrages around Trump might bring them back into the process.

That confidence was on display Saturday before the vote for chairman. DNC members debated whether to strike language from California’s Christine Pelosi, House Minority Leader Nancy Pelosi’s daughter, that would have restored a ban on corporate donations to the DNC. The prohibition was quietly rolled back during the controversial tenure of the previous elected DNC chair, Rep. Debbie Wasserman Schultz (D-Fla.)

“This resolution has nothing to do with nonprofit organizations,” said Larry Cohen, a former president of the Communications Workers of America who backed Sanders in 2016. “This is to send a message, loud and clear, that the DNC itself — not candidates, not state parties — will restore the ban that President Obama put into effect.”

When the language was struck, a few of the activists who had come to cheer Ellison — including members of National Nurses United, Progressive Democrats of America and Democratic Socialists of America — started a brief chant.

“Money out of politics! Money out of politics!”