Khan hailed his victory as the triumph of "hope over fear and unity over division."
His win was the most dramatic result in local and regional elections that produced few big changes but underscored Britain's political divisions ahead of a referendum on whether to remain in the European Union.
Labour Party candidate Khan received more than 1.3 million votes — 57 percent of the total — to Conservative rival Zac Goldsmith's 43 percent, after voters' first and second preferences were allocated.
Turnout was a relatively high 45.6 percent, up from 38 percent in 2012.
Khan's victory seemed certain for hours from partial results, but the official announcement came past midnight — more than 24 hours after polls closed — after delays due to what officials called "small discrepancies" in the count.
Khan was elected to replace Conservative Mayor Boris Johnson after a campaign marked — and many said marred — by U.S.-style negative campaigning. Goldsmith, a wealthy environmentalist, called Khan divisive and accused him of sharing platforms with Islamic extremists — a charge repeated by Prime Minister David Cameron and other senior Conservatives.
Khan, who calls himself "the British Muslim who will take the fight to the extremists," accused Goldsmith of trying to scare and divide voters in a proudly multicultural city of 8.6 million people — more than 1 million of them Muslim.
The attacks, criticized by some senior Conservatives, appear not to have deterred voters from backing Khan. London has seen attacks by Islamic extremists, including July 2005 suicide bombings that killed 52 bus and subway commuters, but has avoided the level of racial and religious tensions seen in some European cities.
"Fear does not make us safer — it only makes us weaker," Khan said in his victory speech. "And the politics of fear is simply not welcome in our city."
Former Conservative strategist Steve Hilton told the BBC that Goldsmith's campaign had brought back "the 'nasty party' label to the Conservative party" — and said Khan's victory sent a "positive and powerful message about London."
Even Goldsmith's sister criticized his tactics. Journalist and socialite Jemima Goldsmith tweeted: "Sad that Zac's campaign did not reflect who I know him to be - an eco-friendly, independent-minded politician with integrity."
Labour, Britain's main opposition party, performed strongly in the capital, taking more than 40 percent of Londoners' votes. That and Khan's victory were bright spots for Labour, which was pushed into third place in Scotland, where it was once dominant.
The Conservatives under popular Scottish leader Ruth Davidson became the main opposition in Scotland's Edinburgh-based parliament — an unprecedented situation in a region that shunned the party for decades.
The pro-independence Scottish National Party secured a third term in government in the county's parliamentary elections, but failed by two seats to retain a majority. That may lessen the party's appetite to push for a new referendum on Scottish independence.
SNP Leader Nicola Sturgeon said the party had "won a clear and unequivocal mandate" and would form a minority government rather than seek a coalition.
While Labour's losses in Scotland were humiliating, the party fared less badly overall than many had predicted. It lost only a handful of council seats and held on to control of major English cities including Birmingham, Newcastle and Sunderland.
Labour leader Jeremy Corbyn said the party had "a lot of building to do" in Scotland, but had "hung on" in England. But the results will do little to soothe restive Labour lawmakers who think Corbyn's left-wing policies are a turn-off for many voters.
In Wales, which has traditionally been pro-Europe, the anti-EU U.K. Independence Party gained seven Welsh Assembly seats, and the party also won two London Assembly seats, their first ever.
Votes were also being counted in the contest for Northern Ireland's Catholic-Protestant power-sharing assembly. Full results there were not expected until later Saturday, but the major British Protestant party, the Democratic Unionists, appeared on course to retain its leading role in power.
Britons will vote on June 23 on whether the country should leave the European Union. Andrew Blick, a constitutional expert at King's College London, said the results underscore how difficult the referendum campaign will be, as attitudes nationally seem to be so complex.
"We don't know where the mood is," he said. "There are lots of different moods. What message do you push ahead with in the campaign when you have so many different opinions?"
Khan sent out Thank You message for the support received
Thank you!
Today was an amazing victory for hope over fear and for unity over division – and it simply would not have been possible without your help.
This victory is not about me. It's about the millions of Londoners whose lives we can improve by building more affordable homes, freezing fares, restoring community policing and cleaning up our toxic air.
Thanks to the time, money and support you gave, we were able to overcome a desperate and nasty Tory campaign. We've sent a strong message that the politics of fear are not welcome in London.
Now the hard work starts. Let's make London even better for all Londoners.
Thanks again,
Sadiq