ABUJA, Nigeria—Boko Haram militants killed at least 20 people overnight on Tuesday, hours after President Muhammadu Buhari fired all Nigeria’s military leadership in a rebuke for their inability to stop the Islamist group’s latest spate of deadly attacks.

Fighters rode motorcycles and pickup trucks into the town of Mainok in northeastern Nigeria late Monday and shot everyone they could find before burning most houses to the ground, resident Modu Kachalla said. “We counted over 20 dead bodies today after we came out of hiding,”...