LONDON — The new mayor of London, Sadiq Khan, said on Wednesday that his election in the face of a divisive campaign highlighting his religion is a lesson to Donald J. Trump that Islam is perfectly compatible with Western values.

Mr. Trump is “playing into the hands of extremists” and is “ignorant about Islam,” Mr. Khan said. “Daesh, ISIS, all those guys, hate the fact that I am mayor of London. Why? Because it contradicts what they say, which is that Western liberal values are incompatible with Islam.”

In a briefing at City Hall and then an interview with The New York Times, Mr. Khan, a member of the Labour Party, said that his Conservative opponent, Zac Goldsmith, “chose a Donald Trump approach and their campaign sought to sow divisions.” But London, he said, “chose unity over division, and a rejection of the politics of fear,” something that he suggested should encourage Hillary Clinton, the probable Democratic candidate for president.

“What we have shown, and I hope it’s a lesson that Hillary and others in America take on board, is hope trumps fear,” he said, adding: “Forgive the pun.”