LONDON— Sadiq Khan, the son of Pakistani immigrants to the U.K., became the capital’s next mayor, in a win that marked a bright spot for the main opposition Labour Party after it suffered losses in other local elections.
Mr. Khan, who became the city’s first Muslim mayor, won by a wide margin, receiving more than 1.3 million votes, while Conservative candidate Zac Goldsmith received 994,614 votes, when counting second-choice preferences.
The campaign had in recent weeks turned bitter. Mr. Goldsmith, the son of a billionaire, had repeatedly accused Mr. Khan, whose father was a bus driver, of being too soft on extremism and questioned his judgment for having appeared at public events alongside people with extremist views. The tactic appeared to backfire, with some Londoners saying they were turned off by the personal attacks on Mr. Khan.
Mr. Khan has said he has represented some unsavory figures through his former career as a human-rights lawyer and said he has spent years fighting extremism.
In a speech just after midnight, Mr. Khan said he promised to be a mayor for all Londoners, regardless of their backgrounds.
“This election hasn’t been without controversy,” he said. “I am proud that London has chosen hope over fear, unity over division.”
Mr. Khan’s win marked a setback for the Conservatives, who had held the post since 2008. Boris Johnson, a prominent Conservative who is campaigning for Britain to leave the European Union, didn’t seek a third term.
Mr. Goldsmith congratulated Mr. Khan but said he was disappointed with the results.
While a win in London was a significant victory for Labour, the wider losses in local councils were a blow to the party’s leader Jeremy Corbyn, who took over the leadership eight months ago after the party suffered a crushing defeat in a general election.
The party saw a notable drop in support in Scotland, which had been a long-term stronghold. After the pro-independence Scottish National Party, which won the most seats, the Conservatives became the second-largest, surpassing Labour.
Labour also faced some losses in England, with most votes counted, though not as many as some pollsters and analysts had expected.
Any loss of seats in a midterm election is considered to be bad sign, since opposition parties traditionally make big gains then as voters express disappointment with the governing party, analysts said.
This was the first time Labour lost seats in midterm local council elections, while in opposition, since 1985.
Mr. Corbyn congratulated Mr. Khan on his win. Commenting on results in other local elections, he said the party “hung on, and we grew support in a lot of places.” He said it needed to do more to rebuild support in Scotland.
Some within his party said the losses around the country show his socialist, left-wing views have alienated more centrist supporters. Others, however, defended him and said he needs more time to prove himself.
John McDonnell, finance spokesman for Labour, told the British Broadcasting Corp. that Labour is laying the foundations for long-term change in the party. “If you compare from when Jeremy took over to now, we are on a clear path of improvement,” Mr. McDonnell said. “That is exactly what our strategy is all about.”
Tim Bale, politics professor at Queen Mary University of London, said the election results likely allow Mr. Corbyn more time as leader, since they are bad but not disastrous. But the poor results aren’t good news for the party overall, he said.
The Conservatives also suffered some local losses. But Prime Minister David Cameron called the results “remarkable,” given that governing parties normally face setbacks in local elections.
The prime minister, who is in his sixth year in office, faces bitter fighting within his party over the U.K.’s continued membership of the European Union ahead of a referendum on the issue next month. The Conservatives also suffered some losses in the local elections.
“Local election day for sitting prime ministers is meant to be a day of dread,” Mr. Cameron said. “But that wasn’t what it was like last night or wasn’t what it was like today.” He said the results showed Labour had “completely lost touch with the people it is supposed to represent.”
By JENNY GROSS