Entering the race immediately after Mr. Trump’s win, Mr. Ellison, a prominent surrogate for Mr. Sanders in the presidential primary race, quickly won support from him and other leading liberals. Allies of Mr. Obama, Mrs. Clinton and other establishment-aligned Democrats soon began casting about for an alternative. In December, Mr. Perez entered the fray, quickly winning praise from Mr. Obama and endorsements from a number of governors.

While voting members of the party are more closely linked to the establishment wing, Mr. Ellison kept the race close by consolidating liberals and picking up support from mainstream Democrats such as the Senate minority leader, Chuck Schumer of New York.

Mr. Perez, though, got a lift in the final days of the contest when the South Carolina Democratic Party chairman, Jaime Harrison, withdrew from the race and threw his support to him. Mr. Perez’s allies said they had enough votes to win on the first ballot, but a single committee member somehow missed the vote.

And Mr. Ellison’s effort was hurt when one of his aides sent a group text message to the committee members claiming that Mayor Pete Buttigieg of South Bend, Ind., who withdrew from the race before the first vote, had endorsed him, only to send a subsequent message with “CORRECTION” in capital letters acknowledging that Mr. Buttigieg had not.

The initial anger from Mr. Ellison’s supporters after the final results were announced was deeply embarrassing to party officials. Former Mayor R. T. Rybak of Minneapolis, the departing vice chairman of the committee, had to wade into the section of jeering activists to quiet them so Mr. Perez could announce his appointment of Mr. Ellison as his deputy.

Party chairman races have in recent years been tidier affairs, with trailing candidates often withdrawing before the election takes place. Indeed, Saturday’s vote marked the first time in over three decades that the outcome of a vote for chairman was unknown when the balloting began.

The attention of Democrats will now turn to a handful of special congressional elections and a pair of promising governor’s races this November in New Jersey and Virginia. But the most significant task ahead for Mr. Perez will come in 2018, when Democrats face a daunting Senate map, a more favorable House landscape and 36 governor’s races, many of which will help determine which party is best positioned to redraw legislative lines after the next census.