Brazil's leftist president, Dilma Rousseff, was narrowly reelected in a vote that split along the country's social class and geography. (Reuters)

Brazilian President Dilma Rousseff was reelected by a whisker in a second-round head-to-head vote, after one of the closest, most aggressive campaigns in the country’s recent history.

Rousseff, whose left-wing Workers’ Party has governed Brazil since 2003, had 51.6 percent with 99 percent of votes counted. Aécio Neves, the center-right candidate for the Brazilian Social Democratic Party, came second, with 48.4 percent.

Cheers rang out and firecrackers exploded in central Rio as the results came in. The election was the top subject of discussion Sunday, with Workers’ Party activists carrying red flags and both sides setting up camps under awnings on city streets.

Rousseff’s party campaigned hard on its social policies, playing down Brazil’s stumbling economy and emphasizing social programs, which have helped to reduce poverty by 55 percent since 2003.

Allan Moreno, 22, who is unemployed, was one of millions who voted for continuity. He was wearing a “Dilma” sticker on his T-shirt when he left a polling station in Glória, an area in central Rio. He voted for Rousseff, he said, “to be sincere, because I’m poor.” He added, “She does more for the people.”

Rousseff supporters celebrate the election results before a news conference in a hotel in Brasilia, Brazil on Oct. 26. (Eraldo Peres/AP)

Rousseff’s victory came after a tough, dramatic campaign marked by sea changes in public opinion. She initially was expected to win easily, but that changed Aug. 13 when another candidate, Eduardo Campos, then polling in third place with 9 percent, was killed in a plane crash. Former environment minister Marina Silva, who came third in the 2010 election, entered the race and quickly took over the lead in opinion polls.

The Workers’ Party targeted Silva in its official election advertising, suggesting in one commercial that her proposals for an autonomous central bank would deliver Brazil to rich bankers and leave poor families hungry.

“The Workers’ Party perceived that Marina was a real threat and made a very strong campaign against her,” said Jairo Nicolau, a political scientist at the Federal University of Rio. Brazilians commonly refer to all candidates, and their president, by first names.

Silva was eliminated in the first-round vote Oct. 5, and the Workers’ Party targeted Neves. Brazilians quickly became polarized between the two parties. A war raged on social media, with friends and even family members falling out over political affiliations and unfriending one another on Facebook. The candidates traded insults in debates. Neves called Rousseff “frivolous” and a “liar”; Rousseff accused him of nepotism.

“It is a level of violence we have never seen before,” Nicolau said.

Rousseff’s hair’s-breadth victory was in no small part the result of the endorsement of the popular Luiz Inácio Lula da Silva, a former union leader who was president for eight years before Rousseff. Lula was highly visible during this campaign, as he was when Rousseff won in 2010, even appearing on posters alongside her.

Rousseff’s support was strongest in lower-income groups, susceptible to her claims that her opponents would end programs such as the Family Allowance income support plan, which benefits about 50 million Brazilians. “She never dropped below a certain level, because of this public,” said Ricardo Ismael, a political scientist at the Pontifical Catholic University of Rio de Janeiro.

A supporter of presidential candidate Aecio Neves cries after listening to the election results in Belo Horizonte, Brazil, on Oct. 26. Neves narrowly lost to incumbent Dilma Rousseff. (Eugenio Savio/AP)

Rousseff also could count on support from left-wing unions and social movements. As campaigning for the runoff became more inflammatory, the election became a matter of taking sides, not just for economic reasons, but also on what many Brazilians considered fundamental principles — broadly speaking, this simplified into social justice vs. neo-liberal economics, even though Neves vowed to maintain the Family Allowance, and Rousseff’s party says it is business-friendly.

The Workers’ Party characterized Neves as the candidate of the rich, white, privileged elite that traditionally ran Brazil. “The theme of the campaign has been whites versus blacks, rich versus poor, and this narrative was constructed by the Workers’ Party,” Nicolau said.

His words were echoed by Gucelia dos Santos, 23, an unemployed mother in Cidade Nova favela in Entre Rios, Bahia state. “He is a mauricinho and filhino do papai,” she said of Neves — effectively, a spoiled rich kid.

Neves was private secretary to his grandfather Tancredo Neves, who was chosen as Brazil’s first post-dictatorship president in an indirect vote but died before he could properly assume office. The younger Neves served two terms as governor of Minas Gerais state, leaving office in 2010 with a 92 percent approval rating.

But he struggled to overcome the image of a “playboy” in the minds of many Brazilians — his second wife, Letícia Weber, is a former model; he dated a former Miss Brazil, Natália Guimarães; and he was forced to answer, and deny, questions about whether he used cocaine.

Marches in favor of Neves by his supporters helped reinforce the stereotype. In São Paulo last week, pro-Neves demonstrators chanted in support of Brazil’s military police, who patrol the streets. This was unlikely to endear Neves to the many Brazilians who regard the police as at best corrupt and violent, and at worst as a throwback to the military dictatorship.

In contrast, some of Rousseff’s campaign imagery played up her past as a member of the armed left-wing resistance to Brazil’s military dictatorship, using an old photograph of Rousseff in thick glasses and the caption “brave heart.”

Born 1947 in Belo Horizonte to a Bulgarian immigrant father and his Brazilian teacher wife, Rousseff was politicized as a teenager, and imprisoned and tortured by the military regime after joining the armed resistance to the dictatorship.

She became first state secretary for mines, energy and communication in Rio Grande do Sul in 1993, then mines and energy minister for Lula from 2003 to 2005, before becoming his chief of staff.

But her reputation as a hard-nosed technocrat has been eroded as Brazil’s economy has not grown as expected under her mandate; the economy entered a “technical recession” in August after two quarters of slight contraction. Inflation, too, has inched above the government’s 6.5 percent target.

As a result, Rousseff’s support was reduced, said Ismael, among self-employed voters in an emergent middle class, and in industrial, financial and agribusiness sectors. “There is a lack of confidence that Dilma is capable of producing more growth in a second mandate,” he said.

Neves had tried to hurt Rousseff over a corruption scandal linking overpayments at Petrobras, the state-controlled oil company, to payments to political parties such as the Workers’ Party. “It is absurd,” said retiree Halina Persercka, 62, after voting for Neves in Rio on Sunday.

New allegations that Rousseff and Lula knew about the scheme were published by the antigovernment news weekly Veja on Friday. The magazine came out a day before it normally goes on sale and had sold out on many Rio newsstands by Sunday. “I reordered twice,” said newsstand owner Almir Tavora, 56.

But the scandal’s impact was not enough to carry Neves to victory. And now Rousseff must unite a country she divided to win.

“I do not believe, from the bottom of my heart, that these elections have divided the country,” she said in her victory speech. “I believe they mobilized ideas and emotions that are at times contradictory.”

Rousseff thanked Lula and promised dialogue and political reform to fight corruption and give impulse to economic growth.

“Brazil, this daughter of yours, once again, will not flee from the fight,” she concluded. “Viva, Brazil!”