Mr. Khan won with 56.8 percent of the vote, versus 43.2 percent for Mr. Goldsmith, according to London’s election body. The results were not final until Saturday morning because in London’s electoral system voters are allowed a first and second preference, and Mr. Khan did not win an outright majority in the first round.
In his acceptance speech, Mr. Khan said that the mayoral election “was not without controversy” and added that he was “proud that London has today chosen hope over fear and unity over division.”
“I hope that we will never be offered such a stark choice again. Fear does not make us safer, it only makes us weaker and the politics of fear is simply not welcome in our city.”
Mr. Khan’s campaign focused on bread-and-butter issues like the cost of housing and transportation. He drew strong support from labor unions and kept a careful distance from his party’s leader, Jeremy Corbyn, a socialist who has an ardent base among young voters but faces heavy resistance among fellow Labour lawmakers.
Mr. Khan argued that, as an observant Muslim, he was well placed to tackle extremism. “I’m a Londoner, I’m European, I’m British, I’m English, I’m of Islamic faith, of Asian origin, of Pakistani heritage, a dad, a husband,” he said in a recent interview with The New York Times.
The fifth of eight children, Mr. Khan was born in Tooting, South London, to recent immigrants from Pakistan, and grew up in a public-housing project. His father drove a bus, and his mother was a seamstress.
Elected to Parliament in 2005, Mr. Khan was appointed a junior minister for communities in 2008, and minister for transport in 2009 under the last Labour prime minister, Gordon Brown. Although he was not one of the highest-ranking ministers, he became the first Muslim to attend cabinet meetings regularly and was admitted to the Privy Council, a largely ceremonial body in which induction normally requires taking an oath to the queen.
“The palace called me and said, ‘What type of Bible do you want to swear on?’ ” Mr. Khan told the magazine The New Statesman. “When I said the Quran, they said, ‘We haven’t got one.’ So I took one with me.”
As London’s mayor, he will have significant power over transportation and planning — as well as responsibilities for the police, civil defense and fire services — in a city with an acute shortage of affordable homes and a creaking, overcrowded mass transit network.
Mr. Khan will succeed Boris Johnson, a Conservative who has held the post since 2008 and is a leading figure in the campaign for Britain’s departure from the European Union. That vote will take place on June 23. Mr. Johnson is seen as a possible successor to Mr. Cameron as leader of the Conservatives, particularly if Britain votes to leave; Mr. Cameron is campaigning for Britain to remain.
Within Britain, the news of the collapse of the Labour Party in Scotland was almost as big as Mr. Khan’s victory.
The Scottish National Party won its third straight victory in the Scottish Parliament — a triumph that its leader, Nicola Sturgeon, called historic, even though the party narrowly lost its majority. The Labour Party fell to a humiliating third place, behind the Conservatives, who won seats in part by appealing to Scots opposed to independence. Scots rejected independence in a 2014 referendum, but there is speculation that a new referendum could be called, particularly if Britain leaves the European Union.
The S.N.P. won 63 seats, down from 69 and two short of the 65 required for a majority in the 129-seat Parliament, but it was far ahead of its rivals. Ms. Sturgeon said she planned to lead a minority government rather than seeking a coalition with smaller parties. The Conservatives captured 31 seats, and Labour 24.
“The Labour Party have completely lost touch with the hard-working people they’re supposed to represent,” Mr. Cameron, the prime minister, said.
In Wales, Labour remained the biggest party, though the U.K. Independence Party, which favors leaving the European Union, entered the Welsh Assembly for the first time, winning seven seats.
In England, Labour won two by-elections to the British Parliament, and a Labour mayor, Joe Anderson, won a second term in Liverpool. Labour also kept control of local councils in crucial English communities like Crawley, Southampton, Norwich and Hastings, where the party had been thought vulnerable.
Nonetheless, Mr. Corbyn’s troubles have taken the focus from the Conservatives, who are deeply divided over the June 23 referendum on European Union membership.
As for Mr. Corbyn, he said that Labour had defied its critics and “hung on” in England, though he conceded that there was “a lot of building to do in Scotland.”