BOSTON — Investigators had been watching Usaamah Abdullah Rahim long enough to know about his avid interest in Islamic State militants, but when they overheard him talking on a cellphone about beheading Massachusetts police officers, they moved in, leading to a confrontation Tuesday morning outside a pharmacy here that left Mr. Rahim dead and once again raised alarms about the influence of foreign extremists on homegrown radicals.

The shooting occurred in the fairly quiet neighborhood of Roslindale on a routine weekday morning, when officials said an F.B.I. agent and a Boston police officer fired on Mr. Rahim after he threatened them with a knife. The shooting quickly and suddenly revealed what officials described as a lengthy terrorism investigation, with several law enforcement agencies looking into a suspected murder plot that involved at least two other people, including a relative of Mr. Rahim’s who was charged Wednesday with conspiracy.

Here in a city that had just finished with the trial of Dzhokhar Tsarnaev, who was sentenced to death a few weeks ago for his role in the 2013 Boston Marathon bombings, the episode prompted another round of outrage and scrutiny. Coming just a month after two Muslim men with ties to the Islamic State were shot and killed while trying to attack an anti-Islamic gathering in Garland, Tex., the case has also renewed concerns in Washington about the long reach of the Islamic State and other radical groups that have seized on Internet recruitment.