BENISHEIK, Nigeria — The men from Boko Haram came tearing through this rural town, setting fire to houses, looting, shooting and yelling, “God is great!” residents and officials said. The gunmen shot motorists point-blank on the road, dragged young men out of homes for execution and ordered citizens to lie down for a fatal bullet.

When it was all over 12 hours later, they said, about 150 people were dead, and even one month later, this once-thriving town of 35,000 is a burned out, empty shell of blackened houses and charred vehicles.

Boko Haram, Nigeria’s homegrown Islamist insurgent movement, remains a deadly threat in the countryside, a militant group eager to prove its jihadi bona fides and increasingly populated by fighters from Mali, Mauritania and Algeria, said the governor of Borno State, Kashim Shettima.

But about 40 miles away in Maiduguri, the sprawling state capital from where the militant group emerged, Boko Haram has been largely defeated for now, according to officials, activists and residents — a remarkable turnaround that has brought thousands of people back to the streets. The city of two million, until recently emptied of thousands of terrified inhabitants, is bustling again after four years of fear.