* Rival Odinga says will challenge vote in court
* International court urges Kenyatta to keep cooperating
* Kenyatta pledges to work with "all nations"
* Police fire teargas at Odinga backers throwing stones
* Disputed election five years ago unleashed tribal violence (Adds Kenyatta, Odinga comments, clashes between police and Odinga supporters)
By Duncan Miriri and George Obulutsa
NAIROBI, March 9 (Reuters) - Uhuru Kenyatta, indicted for crimes against humanity, was declared winner of Kenya's presidential election on Saturday, but rival Raila Odinga said he would challenge the outcome in court and asked supporters to avoid violence.
Kenyatta, Kenya's richest man and son of Kenya's founding president, faces trial after the disputed 2007 presidential vote that unleashed a wave of tribal killings. His win avoided what could have been a divisive a run-off pencilled in for April.
With 51-year-old Kenyatta in the top job, Kenya will become the second African country after Sudan to have a sitting president indicted by the International Criminal Court.
The United States and other Western powers, big donors to the east African nation, said before the vote that a Kenyatta win would complicate diplomatic ties with a nation viewed as a vital ally in the regional battle against militant Islam.
Kenyatta said in his acceptance speech that he and his team would cooperate with international institutions and that he expected the international community to respect Kenya's sovereignty.
"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.
After saying Kenyatta secured 50.07 percent of the vote, edging over the 50 percent needed to avoid a second round, the chairman of the Independent Electoral and Boundaries Commission, Issack Hassan, announced: "I therefore declare Uhuru Kenyatta the duly elected president of the Republic of Kenya."
Shortly afterwards, Hassan handed a certificate of the results to Kenyatta, who had arrived after the declaration. Kenyatta thanked him and went to a nearby university campus in the capital where delivered his acceptance speech.
Many in the election centre cheered, although celebrations started in the early hours of Saturday after provisional results indicated Kenyatta's victory. Supporters thronged the streets of Nairobi and his tribal strongholds, lighting fluorescent flares and waving tree branches and chanting "Uhuru, Uhuru".
Violence flared briefly in Odinga's heartlands where police fired teargas at supporters of the defeated candidate who were throwing stones. "No Raila, no peace," they chanted at the scene near the western city of Kisumu, which was devastated by violence after the 2007 vote.
Odinga, 68, said he would have conceded if the vote was 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.
Odinga, who secured 43.3 percent of the vote, had also questioned the election process before the vote and during the count his party officials had called for tallying to stop.
The election commission, plagued by technical problems that slowed the count, took five days to announce the result. It dismissed accusations of irregularities.
International observers broadly said the vote and count had been transparent so far and the electoral commission, which replaced a discredited body, said it delivered a credible vote.
Kenyatta, the deputy prime minister, climbed above 50 percent by just 8,400 of the more than 12.3 million votes cast.
Both sides relied heavily on their ethnic groups in a nation where tribal loyalties mostly trump 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.
How Western capitals deal with Kenya under Kenyatta and his government will depend on whether Kenyatta and his running mate William Ruto, who is also indicted, work with the tribunal.
U.S. Secretary of State John Kerry said America had longstanding ties with Kenya, and "will continue to be a strong friend and ally of the Kenyan people", and congratulated the people of the east African country for holding peaceful vote.
CALL FOR COOPERATION
Both Kenyatta and Ruto deny the charges and have said they will work to clear their names, though Kenyatta had to fend off jibes during the campaign by Odinga that he would have to run government by Skype from The Hague.
"Until now, Kenyatta has been cooperating with the court and we do hope this will continue," said Fadi El-Abdallah, spokesman for the Hague-based court. "This is part of Kenya respecting its legal obligations under international law."
Kenyans hope the vote, which has so far passed off with only pockets of unrest on voting day, would 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 sought to defuse tension this time and some Odinga supporters said it was time to move on. "I urge our candidate to forget the presidency and let the will of God prevail," cloth vendor Diana Ndonga said.
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.
Others 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 bare-foot car washer on a main Mombasa street. "We hope he can fix infrastructure and security so we have more jobs."
Odinga's camp had said even before the result that they were considering a court challenge. In 2007, he said the courts could not be trusted to handle the case. Kenyatta's camp had also complained about counting delays and other aspects of the vote.
But many Kenyans said this race was more transparent. Turnout reached 86 percent of the 14.3 million eligible voters. (Addigional reporting by James Macharia, Beatrice Gachenge and George Obulutsa in Nairobi, Hezron Ochiel in Kisumu, Drazen Jorgic in Mombasa and Thomas Escritt in Amsterdam; Writing by Richard Lough, Edmund Blair and James Macharia)