The next Kenyan president has pledged to work with international organisations and recognise international obligations after the confirmation of his election victory.

Uhuru Kenyatta, who faces charges of crimes against humanity, won the election by 8,000 votes, which carried him over the 50% threshold to avoid a second round.

Raila Odinga, the prime minister, came second with 43.3% but said he would challenge the results in the courts.

Violence after the 2007 elections caused 1,200 deaths and resulted in Kenyatta being charged with crimes against humanity by the International Criminal Court in The Hague.

Odinga said he would have conceded if the vote had been fair, adding that there was "rampant illegality" in the electoral process and that "democracy was on trial in Kenya" and he would challenge it in court.

"Any violence now could destroy this nation forever, but it would not serve anyone's interests," he said.

In his victory speech, Kenyatta said he expected the world community to respect Kenya's sovereignty and its democratic will. "We recognise and accept our international obligations and we will continue to co-operate with all nations and international institutions – in line with those obligations," he said.

Kenyatta, the current deputy prime minister and the son of Kenya's first president, Jomo Kenyatta, won 50.07% of the vote, just enough to avoid a second round against Odinga.

Both sides relied heavily on their ethnic groups in a nation where tribal loyalties outweigh ideology at the ballot box. Kenyatta is a Kikuyu, the biggest of Kenya's many tribes; Odinga is a Luo. Both had running-mates from other tribes.

John Githongo, a former senior government official turned whistleblower, urged the rival coalitions, Odinga's CORD and Kenyatta's Jubilee, to ensure calm. "Jubilee and CORD, what you and your supporters say now determines continued peace and stability in Kenya. We are watching you!" he said on Twitter.

From the early hours of Saturday after provisional results emerged, Kenyatta's joyous supporters thronged the streets of Nairobi and his tribal strongholds, lighting fluorescent flares and waving tree branches and chanting: "Uhuru, Uhuru".

But tensions rose in the heartlands of Odinga, "No Raila, no peace," Odinga supporters chanted as security forces fired teargas at crowds in Kisumu, a city where violence flared in 2007.

Diplomats believe that Kenyatta must co-operate with The Hague to ensure smooth international relations. "It won't be a headache as long as he co-operates with the ICC," said one western diplomat. "We respect the decision of the majority of the Kenyan voters."

Kenyans hope this vote, which has until now passed off with only pockets of unrest on voting day, will restore their nation's reputation as one of Africa's most stable democracies after killings last time left more than 1,200 dead.

Many Kenyans have said they are determined to avoid a repeat of the post-2007 chaos that brought the economy to a halt. Church leaders in Kisumu, in the west of Kenya that was devastated five years ago, sought to defuse tension this time.

"Our vote was stolen and we're angry," said Denis Onyango, a 28-year-old mechanic, as hundreds of supporter gathered with members of the security forces nearby. "Why did they bring such huge security here if the vote was to be free and fair?"

But some in the city accepted the outcome, more confident this time round that Kenya's institutions had ensured a fair vote. "I urge our candidate to forget the presidency and let the will of God prevail," said Diana Ndonga, a cloth vendor.

Many shops stayed closed as a precaution in the port city of Mombasa, another Odinga stronghold, but streets were calm.

"We are heading for a bleak future where the economy goes down and international relation sour because of the ICC case," said Athumani Yeya, 45, a teacher in the city.

But some were hopeful that Kenyatta could bring change. "We are celebrating. Even with the ICC case in Holland, the people of Kenya still have faith in him," said Thomas Gitau, 25, a barefoot car washer on a main Mombasa street. "We hope he can fix infrastructure and security so we have more jobs."