The New York mayoral race fell into disarray on Tuesday evening after the city’s board of elections admitted it had bungled the count by including more than 130,000 test ballots in its latest tally of votes.

The discrepancy was discovered on a day in which the race for the Democratic nomination appeared to take a dramatic turn: a new count showed Kathryn Garcia moving within striking distance of Eric Adams, the retired police captain and Brooklyn borough president who appeared to have taken a commanding lead after a preliminary tally on election night a week ago.

Instead, there was confusion and chaos as the board removed the latest results from its website after releasing a statement acknowledging a “discrepancy” in the count and pleading for patience. Hours later, it explained that the count had been tainted by a trial of its software that resulted in about 135,000 test ballots being mistakenly added to the mix.

The board apologised, and promised “to ensure the most accurate up-to-date results are reported”.

Still, the blunder unleashed a torrent of abuse on social media on a city agency that has long had a reputation for dysfunction. It comes at a time when voting procedures and irregularities — real or imagined — have moved to the forefront of American politics after a disputed presidential election.

Garcia called the error “deeply troubling” and said it required “a much more transparent and complete explanation”.

The mayor’s race is the first in which New York City has used ranked choice voting. Each voter was permitted to select up to five candidates on their ballot, in order of preference. Losing candidates in the crowded field were then removed, round by round, and their votes reallocated until there were only two candidates remaining.

A preliminary count of ballots on election night showed Adams with 28.8 per cent of the first place votes, followed by Maya Wiley, a former counsel for Mayor Bill de Blasio and the leading progressive candidate, at 19.9 per cent. Then came Garcia, the former head of the city sanitation department, at 17.8 per cent.

That tally was updated on Tuesday afternoon, disqualifying the losers and redistributing their votes. After that process, Garcia had vaulted to second place, with 48.9 per cent of the vote, to Adams’ 51.1 per cent. The margin between them was a scant 15,908 votes, with more than 124,000 absentee ballots still to be counted in the coming days. Wiley fell to third.

Following that release, Garcia issued a confident statement, saying: “Even with today’s ranked choice report we are still waiting for more than 120,000 absentee ballots to be counted and we are confident about a path to victory.”

But Adams’ campaign noticed something amiss: the 941,832 votes announced on Tuesday was at least 140,000 more than the total counted on primary day a week ago.

Adams said the initial count had raised “serious questions”, adding: “We have asked the board of elections to explain such a massive increase and other irregularities before we comment on the ranked choice voting projection.” He later expressed confidence he would still triumph.

The New York contest is being closely watched not only to see who will lead America’s largest city in its recovery from a devastating pandemic but also as the latest battleground in a struggle between the Democratic party establishment and a rising progressive wing.

The Democratic nominee is almost certain to prevail in November’s general election in a city where the party’s voters far outnumber their Republican counterparts.

Garcia, who would be the city’s first woman mayor, billed herself during the campaign as a pragmatist and no-nonsense manager, capable of making an unwieldy city bureaucracy function better. She enjoyed a late burst of support.

Both Garcia and Adams, who would be New York City’s second black mayor, are from the party’s moderate wing. On the key issue of public security, both have argued against defunding the police, promising instead to reform the department while aggressively tackling a steep rise in shootings.