Chief Justice Willy Mutunga announced the decision, which was unanimous, saying the poll was free and fair.
Mr Kenyatta vowed to serve all Kenyans, while his main challenger Prime Minister Raila Odinga, who lodged the appeal, pledged to respect the verdict.
But two people died and 11 were hurt as Odinga supporters clashed with police in his western stronghold of Kisumu.
And there was an angry mood in the Nairobi slum of Kibera, says the BBC’s Gabriel Gatehouse in the city.
Outside the courthouse itself, police used tear gas to chase away people protesting against the ruling but the area became quiet after the court session ended.
Violence after a disputed election in 2007 left more than 1,200 people dead.
The presidential, legislative and municipal elections held on 4 March were the first since the 2007 poll.
Official results said Mr Kenyatta beat Mr Odinga by 50.07% to 43.28%, avoiding a run-off by just 8,100 votes.
Mr Kenyatta and his running mate, William Ruto, are expected to be sworn in as president and vice-president on 9 April.
But they are facing trial on charges of crimes against humanity at the International Criminal Court (ICC) for allegedly fuelling unrest after that election. They deny the charges.
Plea for calm
Supporters of Mr Kenyatta took to the streets of central Nairobi after the verdict, tooting their horns, blowing on vuvuzelas and chanting.
Mr Kenyatta made a televised victory speech hours after the announcement.
“I want to assure all Kenyans… that my government will work with and serve all Kenyans without any discrimination whatsoever,” he said.
“Above all, let us continue to pray for peace in our country.”
He thanked Mr Odinga, who had earlier wished him well.
Petitions had been filed to the court by the prime minister and by civil society groups, who claimed irregularities had affected the election result and called for fresh elections.
In a news conference after the verdict, Mr Odinga expressed “dismay” at the conduct of the election and said that he did not regret making the challenge, but that he fully respected the decision.
“Although we may not agree with some of [the court’s] findings, our belief in constitutionalism remains supreme,” he said.
“We must soldier on in our resolve to reform our politics and our institutions.”
Chief Justice Mutunga said the court’s decisions had been unanimous on all the issues they were asked to rule on.
He said that Mr Kenyatta and Mr Ruto had been “validly elected” in a poll carried out in a “free, fair, transparent and credible” manner.
“It is the decision of the court that the said elections were indeed conducted in compliance with the constitution and the law,” he said.
“At this historical moment in our country, the Supreme Court has discharged its constitutional duty in conformity with the solemn oath each one of us took,” he added.
“It is now for the Kenyan people, their leaders, civil society, the private sector and the media to discharge theirs, to ensure that the unity, peace, sovereignty and prosperity of the nation is preserved. God bless Kenya.”
Outgoing President Mwai Kibaki has urged people to stay calm and accept the result, but much will depend on Kenyans’ faith in their newly reformed judiciary, our correspondent says.
‘Triumph of democracy’
UK Prime Minister David Cameron congratulated Mr Kenyatta on his election victory.
“The prime minister urged the Kenyan people to be proud of the strong signal they have sent to the world about their determination to exercise their democratic right peacefully,” Mr Cameron’s office said in a statement.
A European Union statement said: “Kenyans can be proud that the political reform process of their country is bearing fruit.
“With continued efforts to implement the new constitution, Kenyans can build the globally competitive and prosperous nation they aspire to be.”
The Independent Electoral and Boundaries Commission (IEBC) has insisted that the vote was credible, despite technical failures with an electronic voter ID system and the vote counting mechanism.
International observers said the poll was largely free, fair and credible, and that the electoral commission had conducted its business in an open and transparent manner.