Citing irregularities, the Supreme Court said a new poll should be held within 60 days.
The election commission had declared incumbent President Uhuru Kenyatta the winner of last month’s election.
But Kenya opposition candidate Raila Odinga claimed that the commission’s IT system had been hacked to manipulate the results.
After the ruling was announced, opposition presidential candidate Raila Odinga said: “This is a historic day for the people of Kenya and by extension for the people of the continent of Africa.”
Reading the decision – reached by a majority of four to two among the judges – Chief Justice David Maraga said the 8 August poll had not been “conducted in accordance with the Constitution”.
Opposition supporters were seen celebrating outside the court building, as well as in opposition strongholds.
The election sparked days of sporadic protests in which at least 28 people were killed.
The vote had raised fears of major political violence – as was the case after a disputed election in 2007.