Luiz Inácio Lula da Silva has secured an unprecedented fourth non-consecutive term as Brazil’s president, completing a dramatic comeback against right-wing Senator Flávio Bolsonaro in the October 25 runoff.

Electoral authorities declared Lula the winner after 98.76 percent of ballots had been counted. He received just over 51 percent of the vote, compared with Bolsonaro’s 48 percent. The result reversed the first-round outcome on October 4, when Bolsonaro finished ahead with 47.03 percent to Lula’s 45.16 percent.

A late shift in a close race

The candidates were polling nearly level before the runoff. Bolsonaro briefly led as votes were counted, but Lula moved ahead as returns arrived from the country’s northeast, a longstanding stronghold for his Workers’ Party. Higher turnout may also have helped Lula, after more than 33 million people abstained in the first round.

The victory makes Lula the first candidate in modern Brazilian history to finish second in the opening round and then win the runoff. At 80, he is also set to become the country’s oldest president at inauguration since Brazil returned to democracy.

Voting-hour extension sparks dispute

Confusion marked the final hours of voting. Both campaigns sought an extension: Lula’s team raised concerns about reduced public transport in some cities, while Bolsonaro’s camp pointed to heavy rain and flooding. A Supreme Court justice ordered polling stations that were still open to remain open for two extra hours. Stations that had already closed stayed shut, and some voters said they were turned away.

Bolsonaro questioned the legitimacy of the result and said he would seek to annul it, while accusing the court of changing election rules. Brazil’s Federal Police are investigating alleged transport disruptions in five states. In Salvador, a court ordered the arrest of the city’s transport secretary over allegations of bus-service cuts.

The allegations have not established that the outcome was altered. Lula’s win nevertheless leaves Brazil facing a new round of political and legal confrontation between his supporters, the Bolsonaro camp and a divided judicial system.