Felipe Calderon had 35.88% support with all 41 million votes counted on Thursday. His left-wing rival, Andres Manuel Lopez Obrador, had 35.31% of the vote.
The two were separated by about 220,000 votes.
Calderon led supporters in a party at the ruling National Action Party offices, and called on his adversaries to forget a bad-tempered election that plunged Mexico into political turmoil.
He said: "If the contest is behind us, our differences are behind us. Now is the hour for unity and agreements between Mexicans."
But Lopez Obrador, Mexico City's former mayor, insisted that he won the election, said it was plagued with irregularities and pledged to fight the result in an electoral tribunal.
He called a rally of supporters in Mexico City's central square on Saturday, raising fears of street protests and further unrest as well as weeks of legal wrangling similar to that which followed the US election in 2000.
"We cannot recognise or accept these results," he said.
Lopez Obrador's policies upset
the better-off classes in Mexico
Calderon would be an ally of the United States in Latin America, where reformists have taken power in Argentina, Bolivia, Brazil, Uruguay and Venezuela and turned away from Washington.
Calderon has promised pro-business reforms, more foreign investment and a boom in construction.
Lopez Obrador had led the recount for hours, but Calderon overtook him overnight as the last votes came in from his strongholds in northern and western Mexico.
The narrow margin and months of animosity between the two main candidates have many fearing weeks of legal battles and massive street protests ahead.
"He (Lopez Obrador) is the only person who wanted to help the people"
Lopez Obrador supporter
Guadalupe Tellez, a Lopez Obrador supporter, said: "If a revolution is needed, a revolution there will be. He is the only person who wanted to help the people."
Lopez Obrador was the favourite for most of the campaign, but Calderon closed the gap by smearing his rival, who he depicted as a danger to Mexico's economic stability.
When Calderon was judged to have won a preliminary count earlier this week, Lopez Obrador objected and protests broke out in the capital.
Lopez Obrador pledged to help Mexico's poor with welfare benefits and infrastructure projects to create jobs.
He won wide support in Mexico City but his policies worried investors, business leaders and the middle class.