After the first round of the Brazilian presidential election three weeks ago, the leftist incumbent Luiz Inácio Lula da Silva reportedly told allies he could not understand how he finished second after a government he considered the best of his three terms.

A mood of dismay swept Brazil’s left, and because 9 million voters for the other candidates, all of them on the right, were expected to mostly transfer to senator Flávio Bolsonaro, analysts widely saw the far-right senator’s victory as certain.

Then, on the night after the election, students from the Fluminense Federal University took to the streets of Niterói, a city of 480,000 across Guanabara Bay from Rio de Janeiro, to protest against the return to power of a member of the Bolsonaro family, whose patriarch, former president Jair Bolsonaro, Flávio’s father, is under house arrest for attempting a coup.

Two days later, another student protest took over downtown Rio and, in the days that followed, similar ones spread across the country in organic demonstrations that analysts and Lula himself agree gave new life to his campaign and played a decisive role in his historic comeback in the runoff.

Related: ‘Democracy always’: Lula vows no return to dictatorship in Brazil after election win

“I want to thank the generosity of Brazil’s young people who took to the streets to defend democracy,” Lula said in his victory speech on Sunday night after defeating Bolsonaro.

Political scientist Fábio Vasconcellos said Lula’s victory, in which for the first time in Brazil’s democratic history a second-placed candidate overturned a first-round defeat in the runoff, could be explained by a combination of the streets, social media and the machinery of government – in recent weeks, Lula approved a “package of benefits” at the limits of what electoral law allows, including increasing and bringing forward payments from the main cash-transfer programme for poor families.

“The streets and social media almost overlap,” said Vasconcellos, a researcher with the Representation and Democratic Legitimacy group at the Federal University of Paraná.

Two days after the first round, congressman André Janones, a Lula ally, created a WhatsApp channel called “Lula’s Army”, where Janones sent audio messages, videos – often beginning with shouts of “Guys, urgent!” – and dozens of daily instructions on how to make pro-Lula content perform well on social media.

The channel had 60,000 followers by the end of its first day and, by election Sunday, more than 10 million, and Lula’s digital reach overtook Bolsonaro’s.

In his victory speech, Lula called Janones “our general”, who had the role of “raising the morale of our troops every day”. The congressman’s relentless efforts, which included a 24-hour YouTube live stream on the eve of the vote, led to him being admitted to hospital last week – the only day the WhatsApp channel went silent.

Lula’s campaign also overhauled his online communications, and the incumbent, who turns 81 on Tuesday, began recording selfie videos despite not owning a mobile phone. On Sunday night, he said he had been given a phone and that “from now on I’m going to talk a lot to the Brazilian people, many times a day, directly”.

With the streets and social media, “the mood of defeat was gradually replaced by hopes of a comeback over those three weeks,” said Vasconcellos.

Camila Rocha, a political scientist who studies Brazil’s new right, said Lula’s comeback also represented “a broad rejection of the Bolsonarista project for Brazil”.

Related: Lula supporters celebrate shock election victory – in pictures

She has been interviewing undecided voters since May and said “the fear voters had, workers had, of losing rights and also of a possible concentration of power in Bolsonaro’s hands was very clear”, which meant that even people who did not like Lula voted for him this time.

Bolsonaro had won 56.1m votes in the first round and received 3.7 million more in the runoff. Lula, by contrast, went from 53.9m to 64.2m. The more than 10m-vote increase came both from persuading voters to turn out after historically high abstention in the first round and from convincing voters from other candidates, including Bolsonaro.

“Some undecided voters we interviewed said Bolsonaro was a playboy who sided with big business, while at the same time spontaneously saying that, if he won, there would be a dictatorship in which people would lose rights,” said Rocha.

Bolsonaro was also embroiled in several controversies recently, including saying he would use the new, even more conservative congress to change the constitution and “re-democratise” Brazil, as well as the two federal police inquiries in which he is under investigation over alleged corruption and money laundering.

Many analysts have also pointed to the weight of Lula himself. “If it were another candidate, Bolsonaro would already have won in the first round,” said political scientist Helcimara de Souza Telles.

Marco Aurélio de Carvalho, Lula’s campaign manager, answered without hesitation when asked what explained the historic election fightback: “President Lula. We have a great statesman on our team. We have a star player. That’s what absolutely made the difference. I haven’t the slightest doubt about it.”

The question already occupying the minds of Brazil’s left is what happens next, given that Lula has said this was his last election.

“That is a very big challenge because, in the polling on this, Lula’s dominance is still very strong. There is no one in his party with the same leadership,” said Rocha.

Vasconcellos believes Lula’s re-election campaign, “from what we saw on the streets, offered the Brazilian left an opportunity for renewal”.

“Lula’s party can learn from what happened and find better ways to connect with young people and urban voters who feel their concerns are not being heard,” he added.