That the certification was seen by many as just a step rather than a final result underscored the topsy-turvy nature of the race. On the night of the election, Ms. Cabán had declared victory, citing a 1,100-vote margin. But a delayed count of paper ballots on July 3 put Ms. Katz ahead by 20 votes.

That slim margin automatically triggered a manual recount — the first ever to span a full county, according to state elections officials. Over the past two weeks, Board of Elections staff members crowded into a nondescript mall in Queens — also home to a BJ’s Wholesale Club and a Burger King — to tally every ballot by hand, while volunteers and lawyers for both candidates stood guard.

The recount process itself was painstaking and dull. But it unfolded against a backdrop of mistrust and mutual recrimination, as Ms. Cabán’s supporters hurled accusations of voter fraud and suppression. Ms. Katz’s supporters — including prominent elected officials such as Representative Gregory Meeks — suggested that Ms. Cabán’s backers were gentrifiers inconsiderate of nonwhite voters, many of whom supported Ms. Katz. (Ms. Katz is white, and Ms. Cabán is Latina.)

Ms. Cabán’s supporters have already pointed to the looming court battle, when Ms. Cabán’s lawyers plan to argue that elections officials improperly discarded dozens of ballots — more than enough to flip the election yet again if reinstated.

Many of those ballots were invalidated for technical reasons, such as an eligible Democratic voter showing up to the wrong polling site, or not writing “Democrat” on the ballot. Poll workers are responsible for ensuring that voters fill out affidavit ballots correctly, said Jerry Goldfeder, a lawyer for Ms. Cabán.

Still, it is impossible to know how much those disputed ballots would change the outcome even if they were reinstated. Invalidated ballots were not opened, so there is no way of knowing if they would add to Ms. Cabán’s tally or Ms. Katz’s.

It is unclear how long the court proceedings will take. In 2012, a special election for a Brooklyn State Senate seat led to an automatic recount; David Storobin, a Republican, ultimately won by 16 votes, 72 days after the election.