NOUAKCHOTT, Mauritania — Groups linked to al-Qaeda and the Islamic State, at war with each other in the Middle East, are working together to take control of territory across a vast stretch of West Africa, U.S. and local officials say, sparking fears the regional threat could grow into a global crisis.

Fighters appear to be coordinating attacks and carving out mutually agreed-upon areas of influence in the Sahel, the strip of land beneath the Sahara desert. The rural territory at risk is so large it could “fit multiple Afghanistans and Iraqs,” said Brig. Gen. Dagvin Anderson, head of the U.S. military’s Special Operations arm in Africa.

“What we’ve seen is not just random acts of violence under a terrorist banner but a deliberate campaign that is trying to bring these various groups under a common cause,” he said. “That larger effort then poses a threat to the United States.”

The militants have wielded increasingly sophisticated tactics in recent months as they have rooted deeper into Mali, Niger and Burkina Faso, attacking army bases and dominating villages with surprising force, according to interviews with more than a dozen senior officials and military leaders from the United States, France and West Africa.

To avoid scrutiny from the West, the groups are not declaring “caliphates,” officials said, buying time to train, gather force and plot attacks that could ultimately reach major international targets.

A coalition of al-Qaeda loyalists called JNIM has as many as 2,000 fighters in West Africa, according to a U.S. report released this month. The Islamic State in the Greater Sahara, which staged the 2017 attack that killed four American soldiers in Niger, is also thought to be hundreds strong and recruiting combatants in northeastern Mali.

“This cancer will spread far beyond here if we don’t fight together to end it,” said Gen. Ibrahim Fane, secretary general of Mali’s ministry of defense, whose country has lost more than 100 soldiers in routine clashes since October.

The warnings come as the Pentagon weighs pulling forces from West Africa, where about 1,400 troops provide intelligence and drone support, among other forms of military help. About 4,400 American troops are based in East Africa, where the U.S. military advises African forces fighting al-Shabab ...