Down in the polls, the right-wing president claimed, without evidence, that the election could be rigged. He suggested that he might not accept a loss. And millions of his followers vowed to take to the streets at his command.
But the outcomes, at least so far, have been drastically different.
In Brazil, when the tallies showed that the incumbent had been voted out after just one term, the government responded jointly, swiftly and decisively. The Senate President, the Attorney General, Supreme Court justices and the heads of the electoral agency went on television together and announced the winner. The House Speaker, perhaps the president’s most important ally, then read a statement reiterating that the voters had spoken. Other right-wing politicians quickly followed suit.
Thousands of his supporters took to the streets, blocking highways and demanding a military intervention, but the armed forces have shown no interest in disrupting the electoral process. The demonstrations quickly fizzled and the government began its transition.
In the United States, the aftermath was longer, messier and marked by the worst assault on the Capitol in two centuries. President Donald J. Trump and many of his allies denied that he had lost the 2020 election.
Two years later, the nation faces one of the most dire threats to its democracy in generations, with many Republicans openly rejecting what has been repeatedly shown to be a clean election, including many who promote that lie as they seek office in the midterm election on Tuesday.
The differing pictures raise a fundamental question: Is there something the United States, the world’s oldest democracy, can learn from Brazil, a nation that was emerging from military dictatorship when President Biden first ran for the White House in 1988?
Brazil, for its part, has watched closely what has happened in the United States, where democracy did not break after the 2020 election but it did bend.
With similar chaos forecast for their country this year, Brazilians buttressed their system well ahead of time. Government leaders added additional tests of voting machines and checks of the results, they standardized poll hours so the returns would arrive quickly and they planned to present a united front once a winner was declared.
“We learned from the experience of the United States,” said Bruno Dantas, chief of Brazil’s watchdog court, which completed a rapid audit of vote returns on election night intended to pre-empt fraud claims. “We built a network of institutions that anticipated the questions we knew could arise.”
The speed of Brazil’s vote-counting system was also an important factor.
In many American states, voters use paper ballots, which can slow down counts, and the use of absentee ballots also jumped sharply in 2020 because of the pandemic. The outcome of the election was uncertain for days. By contrast, Brazil is the only country in the world to use a fully digital system without paper backups, which enabled results within hours of the polls closing.
That design was precisely what Mr. Bolsonaro and his allies attacked as a dangerous flaw. They argued that without paper backups, no one could be sure their vote had been counted correctly.
Independent experts agree that paper backups would add assurances, but they also say that multiple layers of security built into the Brazilian system prevent fraud and errors.