Mahama, who replaced former president John Atta Mills after his death in July, took 50.7% of the ballots cast – just enough to avoid a run-off with his chief rival Nana Akufo-Addo.
"Based on the results, I declare President John Dramani Mahama president elect," Ghana Electoral Commission President Kwadwo Afari-Gyan told a news conference in the capital Accra on Sunday.
In a brief speech at his residence following the results, an exhausted-looking Mahama said his win was a "victory for all Ghanaians", and urged the leaders of rival parties to "respect the voice of the people".
Supporters of Mahama drove through the streets of the sprawling seaside capital playing loud music, shouting and honking their horns after the results.
The election marked 30 years of stability and progress in Ghana.
A cliff-hanger election in 2008, in which Akufo-Addo lost by less than 1%, pushed the country to the brink of chaos, with disputes over results driving hundreds of people into the streets with clubs and machetes.
This year's election was fraught with delays after hundreds of newly-introduced electronic fingerprint readers – used to identify voters – failed on Friday and forced some polling stations to reopen on Saturday to clear the backlog.
Security forces used teargas to disperse hundreds of supporters of the Akufo-Addo's New Patriotic Party protesting in front of the electoral commission building shortly before the results were declared.
New Patriotic Party chairman Obetsebi-Lamptey said earlier in the day that he had evidence of electoral workers conspiring to rig tallies and added the party asked the electoral commission for an audit before full results are released.
Mahama has vowed to use rising oil revenues in Ghana, which started oil production in late 2010, to jumpstart development, create jobs and combat poverty.
Akufo-Addo, a British-trained lawyer, had criticised the ruling party for failing to root out government graft and promised to provide free primary and secondary school education.
But in a country where campaign messages rarely influence voting choices, many believe most of Ghana's 14 million voters cast their ballots based on ethnic, social or regional ties.
Ghanaians are also electing a parliament, in which Mahama's party has enjoyed a slim majority. Results were not yet available for those races.
An oil-driven economic boom has brought more wealth to the country, but also fears that it could suffer the graft and turmoil that often plagues energy-rich developing nations.
A New Patriotic Party official was not immediately available to comment on the results, but observers said an official dispute was likely, raising fears of street unrest in the normally tranquil nation.
Ghana's television stations aired long infomercials on Sunday, between election updates, showing clips of wars that have erupted in neighbouring countries interspersed with testimonials from Ghanaians about the importance of maintaining peace.
"This election has been hard, but we must remember Ghanaians are one and we must love each other and remain peaceful," said Wellington Dadzie (69), a former soldier who lives on the outskirts of the capital Accra, before the results.
Ghanaian authorities deployed some 41 000 police and soldiers to secure the election process.
Ghana has had five peaceful and constitutional transfers of power since its last coup in 1981. Its residents like to say "Ghana in peace, not in pieces".
Neighbouring Ivory Coast tipped into civil war last year after a disputed 2010 poll and regional neighbours Mali and Guinea-Bissau have both suffered coups this year.
Oil production in Ghana – which is also a big cocoa and gold producer – started two years ago and oil field operator Tullow Oil says it expects to boost output further in 2013.
"These elections are important not just to Ghana but for the growing number of states and actors seeking to benefit from increasing confidence in Africa," said Alex Vines, Africa Research Director at Chatham House. – Reuters