In a letter to the elections commissioner, Fonseka accused Rajapaksa of using the state media to attack him, misappropriating public funds for his campaign and preventing displaced minority Tamils – whose support Fonseka was counting on - from voting.
The country's election commissioner, Dayananda Dissanayake, appeared to agree with some of the allegations.
He said the state media violated his guidelines, government institutions misbehaved and he pleaded to be allowed to resign.
"I cannot bear this anymore," he said just before announcing the final tally.
Dissanayake also said there were three areas in which vote counters had been assaulted but declined to say which side was responsible.
However, Paikiyasothy Saravanamuttu, of the independent Centre for Monitoring Election Violence, said that though there were reports of irregularities throughout the country, there was no evidence to suggest large-scale fraud during Tuesday's vote.
The United Nations expressed relief that the polls went off relatively peacefully and urged the country's political parties to abide by the official results.
"I had been concerned at the level of violence during the campaign," Ban Ki-moon, the secretary general, said on Wednesday. "I am relieved that the vote yesterday appears to have been relatively peaceful, despite some violent incidents."
He called on all parties "to abide by the decision and rules and regulations, including addressing any electoral grievances".
"I truly hope that all sides will see the wisdom of acting with restraint and responsibility in the interest of the nation. This would bode well for future elections and national harmony," he added.
For much of Wednesday, Sri Lanka's capital was tense as hundreds of soldiers and policemen ringed the upscale Cinnamon Lakeside hotel in central Colombo where Fonseka was staying.
The former army chief who led the military offensive that defeated the separatists, said for hours that he was being held virtual prisoner.
|Dayananda Dissanayake resigned as electoral commissioner as he announced results [AFP]
The military presence appeared designed to prevent Fonseka from stirring up opposition protests to the vote, though there was no indication he had the ability or support to overthrow the government by force.
After night fell, Fonseka left the hotel and went to his private residence, said Tissa Attanayake, the general secretary of the main opposition party.
The soldiers withdrew around the same time.
Rajapaksa said the size of his victory margin proved Fonseka's allegations of vote-rigging false.
"How can you rig 1.8 million votes and why should I rig? I knew from the beginning I was going to get this outcome," he told a news conference.
He called on Sri Lankans to unite for the tough task of rebuilding a nation shattered by a quarter century of ethnic warfare.
"From today onward, I am the president of everyone, whether they voted for me or not," he said.
He also promised to sit down with the Tamil minority to discuss devolution of power, on which he has dragged his feet by citing the need to finish elections first.
But it is the lack of support he received in predominantly Tamil areas of the north - where turnout was extremely low - and his uncompromising stance towards the demands of the long-marginalised minority that signal potential trouble for his next government.
Shortly before Rajapaksa was declared the winner on Wednesday, two people were killed and four wounded in a grenade attack on a Buddhist temple in the central town of Gampola, Brigadier Udaya Nanayakkara, a military spokesman, said.