Dilma Rousseff has been re-elected president of Brazil, after securing more than 51% of votes in the closest election race in many years.
An official count showed her rival, centrist candidate Aecio Neves, taking just over 48% of the vote.
In her victory speech, Ms Rousseff said she wanted to be “a much better president than I have been until now”.
She faced protests last year against corruption, record spending on the football World Cup and poor services.
Ms Rousseff, who has been in power since 2010, is popular with poor Brazilians thanks to her government’s welfare programmes.
But the vote split Latin America’s biggest country almost evenly in two, along lines of social class and geography.
Ms Rousseff called on all Brazilians “to unite in favour of Brazil’s future” and said she would seek political reform.
“This president is open to dialogue. This is the top priority of my second mandate,” she told a cheering crowd in the capital, Brasilia.
She also thanked her supporters, especially her political mentor and predecessor Luiz Inacio Lula da Silva.
“I thank from the bottom of my heart our number one militant, President Lula.”
Her re-election for a second term extends the rule of her Workers Party (PT), which came to power in 2002 with President Lula.
Analysis: Wyre Davies, BBC News, Rio de Janeiro
This has been the tightest of contests but in re-electing Dilma Rousseff, Brazilians have opted for continuity, backing a system and a party that has brought economic growth and generous welfare programmes that have elevated tens of millions of Brazilians out of extreme poverty.
The centrist, business friendly candidate Aecio Neves had pushed Dilma Rousseff hard – his experience as a successful state governor persuading many Brazilians that he could modernize and rationalize what, in recent years, has been a struggling economy.
Brazil looks and feels divided. Whereas Dilma Rousseff did well in the poorer northern states, her opponent took many of the wealthier and more developed southern parts of Brazil.
The challenges facing Dilma Rousseff are huge. International markets are nervous about the high level of government intervention in the economy. Many of those Brazilians whose lives have improved in recent years want more – especially services including better health education and a reduction in Brazil’s chronic levels of crime.
Aecio Neves, of the Brazilian Social Democracy Party (PSDB), admitted defeat in a speech to supporters in the southern city of Belo Horizonte.
He thanked the “more than 50 million Brazilians” who voted for him and said he had telephoned the re-elected president.
“I… wished her success in the administration of her next government, and I reaffirmed what I feel should be our greatest priority, to unite Brazil on the basis of an honourable project which dignifies all Brazilians,” he said at the rally.
Mr Neves was the governor of the southern swing-state of Minas Gerais for eight years.
Both he and Ms Rousseff had made economic growth and lifting Brazilians out of poverty central to their election campaigns.
The election came after weeks of intensive campaigning by the two candidates and a presidential race that took a tragic turn after Eduardo Campos, a main opposition candidate, was killed in a plane crash in August.
His running mate, a renowned environmentalist, Marina Silva, was thrust into his place, vowing to become the South American country’s first “poor, black” president.
But she came third in the first round after Ms Rousseff and Mr Neves secured 41.5% and 33.5% of the vote respectively.
As neither candidate got an absolute majority, the election went into a second round.