President Kenyatta said he would respect the court's decision but also branded the judges "crooks".
Kenya has a history of disputed elections.
"For the first time we have got justice".
After the election, global monitors from the EU, the African Union and the United States had said there was no major fraud on polling day and urged Mr Odinga to concede. The court blamed the Independent Electoral and Boundaries Commission for the irregularities.
The electoral commission of Kenya after a tense election declared incumbent president Uhuru Kenyatta as victor of the polls. At least 24 people, including a 6-month-old baby, died in clashes after the results were announced in August.
Mr Odinga has contested the last three elections and lost each time, and has claimed the votes were marred by rigging on each occasion.
In 2013, Odinga ran again, but Kenyatta won.
Odinga's lawyers also confirmed the identity of private users who gained access to the IEBC system using private mail addresses and uploaded the presidential election forms.
IEBC Chairman Wafula Chebukati has vowed not to resign because he was not "personally touched by the Supreme Court ruling".
The decision was by a majority of the six-man court, with two judges dissenting.
He said this had compromised the "integrity of the entire presidential election".
Citing irregularities, the Supreme Court said a new poll should be held within 60 days.
The commission, he said, would make internal changes ahead of the repeat poll.
"Those commissioners must go".
"Over 8 million people supported the election of Uhuru Kenyatta but the Supreme Court has ignored this in the ruling which is very shameful", said Matheri Wa Hungu. He said those anomalies affected almost 5 million votes.
But lawyers for the election commission and Kenyatta countered that errors were simply "clerical" mistakes and technicalities that did not affect the outcome of the vote. "Kenya just had a hard and controversial election, and this decision pushes it right back into another electoral campaign, which will start nearly immediately".
As news of the court's decision spread, cheers and celebrations could be heard in parts of the capital, Nairobi.