London (CNN) Sadiq Khan, a practicing Muslim and Labour Party politician, has been elected mayor of London, marking a political milestone in the Western world.

Londoners voted in Khan, 45, as the first Muslim mayor of a major Western capital city. He will take office in a metropolis where his fellow Muslims comprise about 12% of the population.

His victory followed an unusually bitter campaign against Conservative candidate Zac Goldsmith, the son of a billionaire, in which race and religion have proven ugly flashpoints.

Congratulations @SadiqKhan . Can't wait to work with you to create a London that is fair for all! #YesWeKhan pic.twitter.com/FqRjfY1xNT

The London-born son of Pakistani immigrants, Khan grew up with his six brothers and sister in a three-bedroom, public housing apartment. He studied law, became a university lecturer and the chairman of a civil liberties group, and was elected to Parliament in 2005.

Affordable housing in a city increasingly drawing the super-rich, aging infrastructure and transportation are top issues facing the new mayor.

Sending congratulations to London's new Mayor and fellow affordable housing advocate, @SadiqKhan. Look forward to working together! — Bill de Blasio (@BilldeBlasio) May 6, 2016

Khan is replacing incumbent Boris Johnson, a colorful and popular figure who took office in 2008 and was a rare Conservative mayor in the Labour-leaning British capital.

Congratulations to @sadiqkhan-1st Muslim Mayor of London- a city for all cultures, backgrounds & religions. A great example to young Muslims — Jemima Goldsmith (@Jemima_Khan) May 6, 2016

Johnson is leading the campaign for Britain to leave the European Union at a referendum on June 23. He is clashing with Prime Minister David Cameron, who is in favor of the United Kingdom remaining.

Photos: The many faces of Boris Johnson London Mayor Boris Johnson arrives at a Conservative Party election rally on May 5, 2015. As London elects a replacement for outgoing Mayor Boris Johnson, look back at some of his most photogenic moments. Hide Caption 1 of 14 Photos: The many faces of Boris Johnson London Mayor Boris Johnson knocks over 10-year-old Toki Sekiguchi during a touch rugby game in Tokyo on October 15, 2015. "I accidentally flattened a 10-year-old on TV unfortunately," Johnson said in a speech to British and American businessmen. "But he bounced back, he put it behind him. The smile returned rapidly to his face." Hide Caption 2 of 14 Photos: The many faces of Boris Johnson Johnson attends the protest of a third runway at London's Heathrow Airport on April 27, 2013. Hide Caption 3 of 14 Photos: The many faces of Boris Johnson Johnson speaks at the Conservative Party Conference in Manchester, England, on October 1, 2013. Hide Caption 4 of 14 Photos: The many faces of Boris Johnson Johnson falls while competing in a tug of war October 27, 2015, during the launch of London Poppy Day. Hide Caption 5 of 14 Photos: The many faces of Boris Johnson Johnson and model Kelly Brook launch a cycling festival in London on August 25, 2011. Hide Caption 6 of 14 Photos: The many faces of Boris Johnson Johnson plays wheelchair tennis to promote a tournament in London on November 24, 2014. Hide Caption 7 of 14 Photos: The many faces of Boris Johnson Johnson poses with members of a Carnival band in London on August 24, 2011. Hide Caption 8 of 14 Photos: The many faces of Boris Johnson Spiritual leader Acharya Swamishree Maharaj marks a bindi on Johnson's forehead during a visit to a new Hindu temple in London on May 28, 2014. Hide Caption 9 of 14 Photos: The many faces of Boris Johnson Johnson holds dance champion Camilla Dallerup at an event in London on July 1, 2010. Hide Caption 10 of 14 Photos: The many faces of Boris Johnson Johnson poses for a photograph to mark the start of a London Underground extension on November 23, 2015. Hide Caption 11 of 14 Photos: The many faces of Boris Johnson Johnson and actress Barbara Windsor play sitting volleyball after a Paralympics match in London on August 31, 2012. Hide Caption 12 of 14 Photos: The many faces of Boris Johnson Johnson takes part in an Olympics news conference on August 13, 2012. Hide Caption 13 of 14 Photos: The many faces of Boris Johnson Johnson eats in the Olympic Village in London on July 12, 2012. Hide Caption 14 of 14

Labour plunges in Scotland, UKIP gets first seats in Wales

The London race was one of several in the United Kingdom on Thursday. Voters also elected candidates for mayoral positions, local council seats, and parliamentary and assembly seats in Scotland and Wales.

The Scottish National Party -- which campaigned for Scotland to leave the UK in 2014 -- won more Scottish Parliament seats than any other party, but failed to secure an outright majority.

The SNP has tonight won an historic third term as Scotland's government. I pledge to be First Minister for all of Scotland. #SP16 — Nicola Sturgeon (@NicolaSturgeon) May 6, 2016

Labour plunged to third, behind the Conservatives -- a result that would have seemed unimaginable several years ago in the former Labour stronghold.

In Wales -- like Scotland, a traditionally left-leaning part of the country -- Labour retained its place as the largest party. But the pro-Brexit, anti-immigration UK Independence Party (UKIP) picked up its first seats there.

UKIP leader Nigel Farage tweeted that "UKIP now stands up for many traditional Labour voters abandoned by Mr. Corbyn's party" -- a reference to Jeremy Corbyn, who became Labour leader last year.

UKIP now stands up for many traditional Labour voters who have been abandoned by Mr. Corbyn's party.https://t.co/HitGK2kYA3 — Nigel Farage (@Nigel_Farage) May 6, 2016

Outrage over terrorist 'friends' article

In London, the mayoral campaign took a particularly vicious turn when Goldsmith, trailing his rival in polls, penned a controversial column May 1 in Britain's Mail on Sunday newspaper.

Zac Goldsmith

Headlined "On Thursday, are we really going to hand the world's greatest city to a Labour Party that thinks terrorists are its friends?" the piece ran with an image of one of London's signature red double-decker buses that had been blown apart in the 2005 terror attacks in the British capital.

The attacks, carried out by Islamist extremists, left 52 people dead.

In the article, Goldsmith accused Khan and the leaders of the Labour Party of having, "whether intentionally or not, repeatedly legitimized those with extremist views."

The piece provoked outrage, and was seen as Islamophobic and unnecessarily divisive in a diverse city whose residents, Muslim and non-Muslim, live under the specter of the ISIS terror attacks that have struck Paris and Brussels in recent months.

The United Kingdom says the current threat level for international terrorism is at severe, meaning an attack is highly likely.

"Tomorrow we must defeat Goldsmith's vile race politics," wrote Maya Goodfellow, a writer for Labour-affiliated blog LabourList, in a comment that was retweeted by Labour MPs.

Even members of Goldsmith's own party were critical.

Baroness Sayeeda Warsi, a Conservative parliamentarian and Muslim, tweeted: "This is not the Zac Goldsmith I know. Are we Conservatives fighting (to) destroy Zac or fighting to win this election?"

Goldsmith, a former journalist, attended the elite Eton College and the Cambridge Centre for Sixth-form Studies, and was editor of The Ecologist magazine for nine years. He was elected to Parliament in 2010.

This is not the @ZacGoldsmith I know.

Are we @Conservatives fighting 2 destroy Zac or fighting to win this election? pic.twitter.com/fzYBUDSjMG — Sayeeda Warsi (@SayeedaWarsi) May 1, 2016

Labour's anti-Semitism scandal

The mayoral campaign has been taking place amid an anti-Semitism scandal gripping Britain's Labour Party, which has seen Labour MPs and councilors suspended after making anti-Jewish remarks.

Critics have accused the left-wing party of being too accommodating of those with anti-Semitic and Islamist views, prompting Corbyn, the party's leader, to set up an independent inquiry into the allegations.

Jeremy Corbyn

One of those accused of having made anti-Semitic remarks was Ken Livingstone, the last Labour figure to serve as London mayor.

David Cameron joined the fray, accusing Khan in Parliament of having shared a platform with an alleged ISIS supporter from his constituency on multiple occasions.

Khan tweeted in response to the accusations: "Disappointed PM has joined Zac Goldsmith's divisive, dog-whistling campaign. I've fought extremism all my life."

The candidate admitted in an interview with The Observer newspaper on Saturday that the anti-Semitism fight had probably hurt his prospects, and said his party needed to do more to confront the problem.

"There are too many examples in our party of people having these views, and action does not appear to have been taken quickly enough," he told the newspaper.

Some Labour figures had argued the scandal was the result of a political smear campaign against the party ahead of local elections.