“No one wants to be the last man on the ground whenever the Kurds, Iraqis or Americans arrive,” said Peter Neumann, the director of the International Center for the Study of Radicalization and Political Violence at King’s College London.

With the fighters aware that the Islamic State is no longer winning, “my prediction is that a majority will first return to Turkey, adding to instability there,” Mr. Neumann added. “Many will then try to return to their home countries. Others will move on to other conflicts.”

European countries are not the only ones that face this peril. This month, France’s defense minister, Jean-Yves Le Drian, warned that Islamic State fighters could flee to Egypt or Tunisia after being driven from their Libyan stronghold, Surt.

“They don’t disappear,” Mr. Le Drian said. “There’s a new risk that appears.”

The number of foreign terrorist fighters flowing into Iraq and Syria — once as high as 2,000 a month — has dropped to a small fraction of that figure in recent months, Western intelligence officials say, as countries crack down on potential fighters and as a shrinking Islamic State territory loses much of its appeal.

But there is another important reason the numbers are down: The Islamic State anticipated its battlefield setbacks and has adjusted accordingly. It has urged many prospective recruits in Europe and North Africa to stay put and carry out jihad at home, arguing that they are more useful as attackers and suicide bombers in their native countries.

In an audio message released on May 21, the Islamic State spokesman Abu Muhammad al-Adnani made clear that the organization would revert to its roots as a guerrilla insurgency. It was an implicit acknowledgment that the Islamic State would eventually lose its strongholds in Syria and Iraq and the very caliphate that has distinguished it from Al Qaeda and other terrorist groups.

Mr. Adnani, who until his death in a Pentagon drone strike in Syria last month also oversaw the Islamic State’s external operations, repeated his call for supporters to stay put and attack the group’s enemies wherever and however possible.