RIO DE JANEIRO — Brazilian voters re-elected Dilma Rousseff as president on Sunday, endorsing a leftist leader who has achieved important gains in reducing poverty and keeping unemployment low over a centrist challenger who castigated her government for a simmering bribery scandal and a sluggish economy.

Ms. Rousseff of the Workers Party took 51.4 percent of the vote in the second and final round of elections, against 48.5 percent for Aécio Neves, a senator from the Social Democracy party and scion of a political family from the state of Minas Gerais, electoral officials said Sunday night with 98 percent of votes in the country counted.

While Ms. Rousseff won by a thin margin, the tumultuous race was marked by accusations of corruption, personal insults and heated debates, revealing climbing polarization in Brazil. Mr. Neves surged into the lead this month in opinion surveys, only to be eclipsed by Ms. Rousseff as the vote on Sunday approached.

“People without much money have seen their lives improve during recent years,” said Liane Lima, 62, a secretary in São Paulo who voted for Ms. Rousseff. “I think we should let Dilma finish what she started.”