Sadiq Khan, a Labour Party lawmaker and a Muslim, has won the election for London's mayor following a campaign marred by charges of anti-Semitism and extremism. (Reuters)

After a bitter and bruising race, the Labour Party’s Sadiq Khan won London’s mayoral race early Saturday and made history as the first Muslim elected to lead a major Western capital city.

The outcome is likely to resonate far beyond a change in London’s City Hall, challenging the rise of anti-Islam political rhetoric in the West and giving another powerful voice to Britain’s large Pakistani community just when the country is facing its own identity crisis.

Khan, who takes over as mayor of a city of 8.6 million that normally votes Labour, hailed his victory as the triumph of “hope over fear and unity over division.”

At a time when Islam is seen by many in the West as politically toxic, Khan embraced it to winning effect.

Eleven years ago, Pakistanis in Britain faced suspicions and huge public backlash after coordinated terrorist bombings of the London transit system killed 52 people and wounded more than 700.

Sadiq Khan begins first full day as mayor of London after a landslide victory. (Reuters)

Yet Khan has never tried to hide his faith. He memorably tweeted to his main rival, the Conservative’s Party’s Zac ­Goldsmith, at one point: “Hey @ZacGoldsmith. There’s no need to keep pointing at me & shouting ‘he’s a Muslim.’ I put it on my own leaflets.”

He told The Washington Post on the campaign trail this week: “I want to be the British Muslim that defeats the extremists, defeats the radicals. I’ve got a plan of how we can keep London safe.”

Election officials said Khan defeated Goldsmith by more than 300,000 votes, after first- and ­second-preference votes were allocated, the AP reported. The result came early Saturday, more than 24 hours after polls closed. Khan will replace Boris Johnson, a Conservative.

The mayoral election took place just six weeks before Britons will vote on whether to stay in the European Union — a question that has raised sharp political divisions on issues that include control over immigration policies.

London’s mayor wields less direct power than big-city counterparts in the United States and elsewhere. But the position has gained a higher profile as a soapbox for various issues — even international affairs — under previous mayors such as the left­-
leaning Ken Livingstone and Johnson.

Sadiq Khan canvasses for supporters at a market in London on Wednesday. (Justin Tallis/AFP/Getty Images)

The two front-runners came from strikingly different backgrounds.

Khan, 45, the son of a Pakistani bus driver and seamstress, trained as a lawyer before becoming a Labour Party member of Parliament for Tooting, a south London constituency. He slept in a bunk bed at his parents’ home until he was 24.

Goldsmith, 41, is a longtime environmentalist and son of a billionaire financier. He is currently a lawmaker from Richmond in south London. His sister is Jemima Khan, whose ­ex-husband, Imran Khan, is a former cricket star from Pakistan who has since turned to politics in that country.

Jemima Khan appeared to criticize the campaign of her brother, who repeatedly accused Sadiq Khan of giving “oxygen and cover” to Islamic extremists.

“Sad that Zac’s campaign did not reflect who I know him to be — an eco friendly, independent-minded politician with integrity,” she tweeted.

The mayoral race was one of a number of local and regional elections across Britain in what were dubbed the “Super Thursday” contests.

Outside London, the Labour Party suffered losses in various parts of the country, suggesting it has a steep hill to climb to win the next general election in 2020.

This week’s votes marked the first big set of elections since Jeremy Corbyn, a politician on Labour’s far left, won his party’s leadership contest last fall with massive grass-roots support.

And the result? A mixed bag.

Opposition parties normally do well in local elections, but Labour did not. However, it did not lose as much ground in England as some pundits had projected, and it remained the dominant party in the Welsh assembly.

But it fell behind the Conservatives to become the third-largest party in Scotland.

The Conservatives have spent decades in the political wilderness in Scotland, but they made gains — long considered unthinkable — under the party’s charismatic leader there, Ruth Davidson.

Nicola Sturgeon’s Scottish National Party was set for a third term in power in the Scottish Parliament — it was just shy of a securing a majority — inevitably raising questions about the possibility of a second independence referendum for that region.

The local elections come at a time when the leaders of the two main parties, the Conservatives and Labour, have been locking horns in increasingly acrimonious exchanges.

British Prime Minister David Cameron, leader of the Conservatives, accused Corbyn’s Labour Party this week of having a problem with anti-Semitism, following a scandal that has rocked the party. The Labour Party recently suspended a senior member over controversial remarks.

Cameron also questioned Khan’s judgment — not for the first time — saying that he had shared platforms with terrorist sympathizers.

For his part, Corbyn accused the Conservatives of “smearing” Khan in the mayoral race.

The mayoral contest in London, where the city’s housing crisis was the main battleground during the campaign, turned increasingly ugly as it neared the finish line.

Khan was forced to deny claims that he supports Islamic extremists (he says he met with “unsavory characters” as part of his job as a human rights lawyer), and Goldsmith was compelled to reject claims that he is a racist (he says he raised legitimate questions).

When asked whether the Conservatives’ mayoral campaign had done “lasting damage” to relations with London’s Muslim community, Andrew Boff, the Conservative leader of the London Assembly, a mayoral watchdog, said: “I think it has.”

“Welcome to politics of ethnicity in a diverse city,” said John Curtice, a professor at the University of Strathclyde. “We know that Labour Party does relatively well in London amongst ethnic minorities. We also know that we are talking about a society in which there is a fair degree of Islamophobia, and if a party is behind and is playing dog-whistle politics, don’t be surprised.”

In London, a cosmopolitan city with more than 1 million Muslims, the negative campaigning seemed to leave a bitter taste for some.

Speaking after she voted Thursday at a polling station in Khan’s Tooting borough, Kate Parry, 50, a market researcher, said the tone of the debate had “backfired on the Conservative campaign.”