Uhuru Kenyatta, who faces trial for crimes against humanity, won Kenya’s presidential election by the narrowest of margins, 50.07 percent of the vote, the country’s electoral commission announced Saturday.

But whether the 51-year-old deputy prime minister can take office remained far from clear Saturday as his main rival, Prime Minister Raila Odinga, alleged “massive tampering” and “irregularities” with the counting of votes and declared that he would challenge the results in Kenya’s Supreme Court

“It is clear that the constitutionally sanctioned process of electing a new set of leaders to take us to the next level has been thwarted by another tainted election,” Odinga told reporters. “It is democracy that is on trial.”

This was Kenya’s first election since the disputed 2007 election, when Odinga also declared that he was cheated out of victory, triggering ethnic violence that killed more than 1,000 Kenyans and drove several hundred thousand others from their homes. The mayhem crippled Kenya’s economy and shattered its image as a bulwark of stability in Africa.

Kenyatta, the son of Kenya’s founding father, Jomo Kenyatta, reached the 50 percent needed to avoid a runoff by winning 6,173,433 votes out of more than 12.3 million cast. Odinga won 5,340,546 votes, 43.3 percent, in an election contested by eight candidates. About 8,000 votes pushed Kenyatta above the 50-percent mark. The turnout, at 86 percent, was the highest ever in Kenya’s history.

Most of the voting predictably went along ethnic lines, with the vast majority of Kikuyus choosing Kenyatta, while Luos cast their ballots for Odinga.

Even if the Supreme Court upholds Kenyatta’s victory, he is expected to face a highly complicated term in office. Both he and running mate William Ruto are accused by the International Criminal Court at The Hague of financing and instigating ethnic mobs who carried out mass killings after the 2007 election. That means both men would have to endure months, if not years, on trial while running the country.

Their trial, originally scheduled for next month, has been postponed to July 9. Both men have denied the allegations and have said they would cooperate with the court to clear their names.

Relationships with key allies and donors could prove to be awkward. The United States and European nations have already warned that there could be consequences if Kenyans chose a leader facing charges by the international court. Some Western diplomats have said they would only have essential discussions with Kenyatta if he were elected.

Yet Kenya is a key ally of the United States and an ancestral home of President Obama. Kenya has played an important role in the fight against Somalia’s al-Qaeda-linked al-Shabab militia. Kenya’s economic stability is also vital to the region’s and the continent’s growth.

On Saturday, Secretary of State John F. Kerry congratulated Kenya on holding peaceful elections and urged all the political parties “to peacefully address any disputes” through the courts, “rather than on the streets.” Noticeably, Kerry did not once mention Kenyatta.

At a news conference Saturday, Kenyatta said he would comply with the ICC and attend hearings at The Hague. But he also appeared to suggest that the world should respect that Kenyans have chosen him and Ruto as their leaders and not apply pressure.

“We recognize and accept our international obligations, and we will continue to cooperate with all nations and international institutions in line with those obligations,” Kenyatta said. “However, we also expect that the international community will respect our sovereignty and the democratic will of the people of Kenya.”

International elections observers have declared the election transparent despite a host of technical problems and delays. The computers generating the electronic transmission of results broke down, forcing the electoral commission to manually count the votes, which took four days.

Odinga said his party has “highlighted so many irregularities in the tallying process” and brought them to the attention of Kenya’s Independent Election and Boundaries Commission. Odinga added that his party asked for a recount, but the request was denied.

“We thought this would never happen again,” Odinga said. But he also urged his followers not to resort to attacks or any form of chaos like what unfolded after the 2007 results.

“Any violence now could destroy this nation forever,” he said. “That will not serve anyone’s interest. Please therefore look upon each other as brothers and sisters whose national bond should not be broken.”

Odinga added that he would accept the Supreme Court’s decision, even if it favored his rival. “Nothing would have pleased me more if I’d lost fairly,” he said.

Jamleck Kamau, a Kenyatta adviser, said they were prepared to “defend” any legal challenge brought by Odinga.

Around Nairobi, the mood was a meld of happiness and frustration. People danced in the narrow downtown streets, waving campaign banners emblazoned with the images of Kenyatta and Ruto — or “UHURUTO” as the pair was called. Minivans jampacked with supporters, dressed in red campaign T-shirts, paraded in many nooks of the capital.

But in the slum of Mathare, security was tight. Trucks filled with heavily armed government security forces patrolled the streets, as a police helicopter hovered above the area. Here, Kikuyus and Luos live side by side and bitterly fought each other after the 2007 vote. On Saturday, scores of Luo youth on the streets expressed anger and disappointment. “They tampered with the election,” declared Robert Ouko, 29, as a friend nodded in agreement.

Others brought up the ICC case, saying it would hinder Kenyatta’s ability to govern Kenya. “We cannot have a leader who is a criminal,” said Morris Regge, 26, a carpenter.

“He’s going to lead the country Skyping from The Hague?” chimed in Regge’s friend David Ollo, 28.

As they spoke, other youths yelled in the street: “We don’t want Uhuru” and “No Raila, No Peace.”

But Kenyatta’s supporters said he was innocent, and the case was a conspiracy to undermine him. Some expressed anger at Odinga’s decision to challenge results. When asked what would happen if the court ruled against Kenyatta, Martin Kuthia, 18, warned, “There will be an ethnic war.”