Governing party candidate Felipe Calderon has been formally declared Mexico's president-elect after two months of political uncertainty.
Mr Calderon is due to take office on 1 December
Seven judges at Mexico's top electoral court ruled unanimously that he had narrowly won the 2 July poll.
They rejected allegations by the losing candidate, Andres Manuel Lopez Obrador, that there had been widespread fraud.
Mr Lopez Obrador, whose supporters have been protesting for weeks, said he did not accept the ruling.
"I do not recognize someone who tries to act as the chief
federal executive without having a legitimate and
democratic representation," he told supporters in Mexico City's main square, the zocalo.
Mr Calderon, who will take office on 1 December for a single six-year term, is to give a nationwide radio and television address later on Tuesday.
The court decision came after weeks of legal wrangling over the closest presidential election in Mexican history.
The judges all voted in favour of a recommendation to declare Mr Calderon, candidate of the governing National Action Party (PAN), president-elect and reject allegations by Mr Lopez Obrador that the campaign had been unfair.
The final vote count gave Mr Calderon victory by 233,831 votes out of a total of some 41.6m cast.
The judges criticised political advertising during the campaign and the conduct of outgoing President Vicente Fox, but said this was not enough to declare the poll void.
The court's ruling is final
Outside the court in Mexico City, Mr Lopez Obrador's supporters wept and set off fireworks that could be heard inside as the judges announced their decision.
"This has been fraudulent from start to finish," Claudio Martinez told the Associated Press news agency.
The court's president, Leonel Castillo urged Mexicans to unite and heal the deep divisions exposed by the election and its aftermath.
"I hope we conclude this electoral process leaving confrontation behind," he said.
But that is unlikely in the bitter political climate, correspondents say.
Last Friday, there were chaotic scenes in the Congress when dozens of opposition deputies took over the podium and prevented President Fox from making his state-of-the-nation speech.
Lopez Obrador supporters were hoping for a reversal of the result
Since the election, Mr Lopez Obrador's supporters have been almost permanently camped out in the capital's main square.
Thousands of Mexicans turned out on Sunday at a rally in Mexico City in which Mr Lopez Obrador declared he would go ahead and set up what he called a "national democratic convention" on 16 September - Mexico's Independence Day.
He has already hinted at establishing a parallel government.
For his part, Mr Calderon has already spoken of the need to bring Mexicans together.
And on Tuesday, his campaign manager Josefina Vazquez Mota said the president-elect was ready for dialogue.
Mr Calderon, she said, "will listen to the voices of all those who exercised their right to vote for a different option".
But Mr Lopez Obrador's supporters have rejected any ideas of dialogue.
"The only possibility for a dialogue with the right's candidate would be for (Mr Calderon) to refuse the gift of the presidency which he did not earn at the ballot box," said Gerardo Fernandez Norona, spokesman for Mr Lopez Obrador's Party of the Democratic Revolution (PRD).