The Aug. 8 election was conducted peacefully and was largely praised by international observers. But David Maraga, the court’s chief justice, declared the result “invalid, null and void” after siding with the opposition, which had argued that the vote had been hacked and electronically manipulated to assure a victory for President Uhuru Kenyatta.
Mr. Kenyatta, 55, was re-elected with 54 percent of the vote, easily surpassing the 50 percent threshold needed to avoid a runoff. His main challenger, Raila Odinga, 72, who had petitioned the Supreme Court to nullify the election, received about 44 percent, a difference of about 1.4 million votes. A parallel tally by domestic observers endorsed the official result.
The court decision came as a surprise, even for Mr. Odinga and his supporters, who had complained about election irregularities: A top election official in charge of voting technology was murdered about a week before the election, and although the casting of ballots went smoothly, parts of their collation and electronic transmission were flawed, leading the opposition to assert that as many as seven million votes had been stolen.
“Irregularities affected the integrity of the poll,” Justice Maraga told a stunned courtroom. The Independent Electoral and Boundaries Commission, which was in charge of the vote, “failed to conduct the election in the manner set in the constitution,” he said, although the six-judge panel found no misconduct on the part of Mr. Kenyatta.
Thousands of supporters in the opposition strongholds of Kisumu, Mombasa, and parts of Nairobi streamed into the streets, whooping with joy while supporters of Mr. Kenyatta in Gatundu, his hometown, were subdued.
“I am happy to be Kenyan today,” said Mr. Odinga, a former prime minister who was making his fourth run for the presidency. “It is a historic day for the people of Kenya and by extension the people of Africa.” he added.
It was the first time in the history of African democratization, he said, in which “a ruling has been made by a court nullifying irregular presidential elections. This is a precedent-setting ruling.”
Security had been increased on Friday in opposition strongholds, amid concern that a ruling in favor of either side could provoke protests or worse. Kenya has a history of postelection violence following presidential votes in 2007, 2013 and last month, when at least 24 people were killed, most of them by security forces.
“My concern is that no matter what the court says, the losers will react violently,” John Campbell, a senior fellow for Africa policy at the Council on Foreign Relations and a former ambassador to Nigeria, said before the ruling.
Mr. Campbell expressed concern that “neither Kenyatta nor Odinga prepared their followers for the possibility of losing.” Immediately after the court’s announcement, however, the atmosphere was more of joy than fear, and there were no immediate reports of violence in strongholds on the losing side, including Gatundu.
Both sides went to court holding strident positions: The opposition said it had evidence of “monstrous fraud and forgery” and the incumbent said it was “bullish and ready” to strike down its rival’s claims.
The Supreme Court, which has bolstered its independence in recent years but had been — at least until now — viewed by many Kenyans as being under government influence, was facing pressure to set out arguments that would persuade people on both sides, said Dickson Omondi, a country director for the National Democratic Institute, a nonpartisan organization that supports democratic institutions and practices worldwide.
The court case was an opportunity for the judiciary to truly show its independence, Mr. Omondi said, after it came under intense criticism of its mishandling of a similar petition by Mr. Odinga in 2013, and which the presiding judge alluded to on Friday at the start of his remarks.
Most African courts are often under pressure from leaders, Mr. Omondi said, so “It’s a historic moment showing the fortitude and courage of the Kenyan judiciary.” He also applauded other figures and groups involved — Mr. Kenyatta and the electrical commission — for appearing to have accepted the judges’ decision, and said the ruling holds the potential to influence similar cases that arise elsewhere in Africa.
The election controversy hinged on two paper forms that legally validate the ballots — one from each of the country’s 40,883 polling stations and the other from 290 constituencies. Representatives from rival parties were required to sign off on the forms before the papers were scanned and electronically transmitted to a national tallying center in Nairobi, where they were to be put online immediately so they could be crosschecked by everybody.
But the electronic system, which had been overseen by Christopher Chege Msando, the election official who was murdered, broke down. As a result, only the results were sent to the national tallying center, often via text message.
International election observers were quick to praise the electoral body after the vote, mainly based on the apparent lack of evidence that votes had been tampered with at polling stations and that the paper forms, not the electronic transmission of results, reflected the integrity of the election.
But when Mr. Kenyatta was initially declared the winner, just hours after voting ended, almost none of the forms from the polling stations were online, even though the electoral commission had a week to receive scanned images of the results. A couple of days later, the electoral commission announced that about 10,000 forms were unaccounted for, sowing even more doubt and suspicion over its credibility.
“The scenario was similar to that of the Bermuda Triangle where no one knows how ships disappear,” said Pheroze Nowrojee, a lawyer representing Mr. Odinga and the National Super Alliance, the opposition umbrella group.
The electoral commission said it had presented the forms, cited in a report by the registrar of the Supreme Court. However, that report found that a third of the forms lacked security features like watermarks or serial numbers, which election observers saw as evidence that the forms were probably false.
Mr. Kenyatta’s lawyer, Fred Ngatia, denied any wrongdoing, saying that what “had been presented as defective were correct and did not have any error.”
The opposition was also not given enough time to access, as ordered by the Supreme Court, the electoral commission’s servers, logs, and the electronic kits used to identify voters and transmit the results.
Walter Mebane, a professor of statistics and political science at the University of Michigan who studies elections worldwide, volunteered to run the voting results through a computer model he developed to detect electoral fraud. Based on statistics only, and without knowledge of the intricacies of Kenyan politics, he and his team found patterns that showed widespread manipulation.
“It was unlike any data set I had ever seen,” he said. “Every single indicator came up signaling anomalies. It’s a huge red flag that something weird is going on.”