Mr. Kenyatta, who has been accused by prosecutors at the International Criminal Court in The Hague of bankrolling death squads during Kenya’s last election crisis, cleared the 50 percent threshold and avoided a runoff by the thinnest of margins, about 8,000 votes out of 12 million, or .07 percent.
Mr. Kenyatta’s trial is set for July, which means that Kenya, one of the United States’ closest allies on the continent, could soon have a president commuting back and forth from The Hague, simultaneously trying to run a country and keep himself from being jailed for years.
 
Mr. Kenyatta has said that he is innocent and that he will cooperate with the court, but in his acceptance speech on Saturday he signaled that he wanted the world to back off.
 
“We recognize and accept our international obligations,” he said. “However, we also expect that the international community will respect the sovereignty and the democratic will of the people of Kenya.”
 
But it is not clear whether Kenya’s election is really over. The second-place finisher, Raila Odinga, Kenya’s prime minister, has refused to admit defeat and plans to appeal to Kenya’s Supreme Court to overturn the results, which some independent observers said were suspicious. Mr. Odinga said there had been “rampant illegality” and “massive tampering” with the vote-tallying process, the same problem that happened in 2007 during Kenya’s last election. Mr. Odinga narrowly lost that vote, and Kenya exploded in political violence.
 
“We thought this would never happen again,” he said, referring to the fraud allegations.
But he urged his supporters to stay calm.
 
“We don’t want riots, property damage or any other kind of disturbance,” Mr. Odinga said. “Any violence now could destroy this nation forever.”
 
Some of Mr. Odinga’s strongholds, like the sprawling Kibera slum, were tense on Saturday. Police officers in helmets prowled the streets as groups of young men glared at television sets, many still visibly dumbfounded by the election results, but there were no reports of major violence.
Mr. Kenyatta’s victory, after a heated race, poses unique challenges for the Obama administration, which increasingly relies on Kenya as a strategic partner in a volatile region, yet, at the same time, has pledged to support the International Criminal Court, though the United States is not a member.
In the weeks before the election, the American top official for Africa issued a thinly veiled warning that “choices have consequences” and some Western diplomats here indicated that they would keep their distance from Mr. Kenyatta, currently a deputy prime minister, should he win. Many analysts predict that the coming months will be an awkward time for United States-Kenya relations, unexpected, perhaps, considering that Kenya was the birthplace of President Obama’s father and a place Mr. Obama has written fondly about.
 
The International Criminal Court — and the perception that Western countries were lining up against Mr. Kenyatta — seems to have been a galvanizing factor in this election, driving Mr. Kenyatta’s supporters to the polls in a tsunami-like force. In some constituencies, especially in the areas of his Kikuyu ethnic group, he won 98 percent of the vote.
 
“Kenyans may be tribalists but we are also nationalists,” said Edward Kirathe, a real estate developer and a passionate Kenyatta supporter. “If someone from the outside tells you not to select someone, then that is the person you go and select.”
 
Mr. Kenyatta’s running mate, William Ruto, one of Kenya’s most charismatic politicians, who is in line to become deputy president, has also been charged by the International Criminal Court with crimes against humanity, accused of sponsoring gangs of young men who hunted down supporters of his political opponents in 2007 and early 2008.
 
On Saturday, Nairobi, Kenya’s capital, was split between scenes of jubilation and scenes of grief.
On River Road, in downtown Nairobi, pickup trucks chugged through the streets, loaded with Kenyatta supporters draped in red — Mr. Kenyatta’s campaign color. They blew horns, danced joyous jigs and shouted “amani!” or peace, while police trucks crawled slowly behind them. In Uhuru Park (uhuru means freedom in Swahili, which is how Mr. Kenyatta got his name, because he was born shortly before Kenya’s independence in 1963), thousands of supporters waved freshly chopped branches, turning a parade ground into a sea of piney-smelling green.
 
But the mood could not have been more different in Kibera, where thousands of Mr. Odinga’s supporters live in sheet-metal shacks. Many young men were huddled on the street corners, simmering with disappointment and anger.
 
“This was rigged,” said Fred Okech, a math teacher.
 
In one part of the slum, residents said shouting matches erupted between supporters of Mr. Kenyatta and Mr. Odinga.
 
“There’s tension,” said Jacob Otieno, a college student. “I think there might be clashes because this was not fair.”
      
Police officers had set up checkpoints all around the slum, stopping cars, peering into trunks and looking for weapons, while a helicopter circled overhead.
      
This election was always expected to be close. Polls had showed Mr. Odinga, 68, and Mr. Kenyatta, 51, neck and neck. Both won many fans outside their ethnic groups (Mr. Odinga is a Luo) but much of their support came from their ethnic strongholds where in some places nearly every single voter voted for his or her favorite.
      
It seems Mr. Kenyatta’s vast family fortune, estimated to be around $500 million, definitely helped, with so many parts of Nairobi and smaller towns literally painted red with “UHURUTO” campaign posters and towering billboards. Mr. Kenyatta’s father, Jomo, appropriated hundreds of thousands of acres of the choicest tracts of land across the country, planting deep grievances among other ethnic groups. But Mr. Odinga is no pauper either. His father, Jaramogi, was Kenya’s first vice president, and the Odingas have vast holdings, too.
      
On election day, this past Monday, millions of voters flooded into poll places, some waiting 10 hours in line, with no food or drink. Most observers said the voting itself went fine. But problems began to crop up as soon as the polls closed and the newfangled vote transmission system, in which results were supposed to be relayed via encrypted text messages, crashed. The election commission then spent the next four days tabulating results manually, which spawned all sorts of conspiracy theories and paralyzed this country, because many Kenyans stayed at home, away from school and work, awaiting the results.
 
After the election commission declared Mr. Kenyatta the winner, the American government issued a statement congratulating “the people of Kenya for voting peacefully” but, pointedly enough, making no mention of the president-elect.
Mr. Kenyatta, a graduate of Amherst College and a smooth, confident speaker, said that Kenya had surpassed expectations.
“We dutifully turned out, we voted in peace, we upheld order,” he said. “That, ladies and gentleman,” he emphasized, “is the real victory.”