Cameron's comments were aimed at Sadiq Khan, a Muslim former government minister and human rights lawyer who is leading opinion polls for the May 5 mayoral election.
During Prime Minister's Questions in the House of Commons, Cameron accused Khan of appearing alongside imam Sulaiman Ghani on nine occasions.
"If we are going to condemn not just violent extremism but also the extremism that seeks to justify violence in any way, it is very important that we do not back these people and we do not appear on platforms with them," Cameron said.
Cameron's comments were interrupted by an uproar from Labour MPs, some of whom shouted "racist" at him.
Also Read
Labour leader Jeremy Corbyn called the comments "disgraceful" while a Labour source said it "demeans the office of prime minister" to repeat such allegations.
Khan, the son of a bus driver who moved to Britain from Pakistan, accused Cameron's Conservatives of running a "nasty, dog-whistling campaign that is designed to divide London's communities".
He said on BBC television this week that his work as a human rights lawyer had brought him into contact with some extremists.
A Downing Street source said that Cameron had been raising an issue about Khan's "judgment" and rejected claims of racism as "complete nonsense".
An Opinium poll for the London Evening Standard newspaper this month gave Khan a clear lead with 35 percent of first preference votes compared to 27 percent for Zac Goldsmith, the candidate for Cameron's Conservative party.
The race to succeed Conservative Boris Johnson as London mayor has often been a bitter and charged one.