The clashes took place in Sabon Gari village when the militants ambushed troops returning to a military base, military spokesman Major-General Edward Buba said on Wednesday.
The terrorists belonged to the Boko Haram and ISWAP groups, he said. They were riding on motorcycles and trucks mounted with guns when they carried out the attack on Saturday.
The troops, along with reinforcements from the Civilian Joint Taskforce volunteers and vigilante groups, successfully repelled the attack, he said.
Fleeing terrorists
Nigeria has been grappling with a 16-year-long insurgency in its northeast driven primarily by Boko Haram and its offshoot ISWAP that has led to huge human and economic losses, including mass displacement and a humanitarian crisis.
Buba said six soldier were killed in the action while an improvised explosive device (IED) injured the vigilante commander.
The Nigerian Air Force also carried out airstrikes on the fleeing insurgents, resulting in further casualties in addition to the 34 killed in the gun battle, Buba said.
➤Click here to follow our WhatsApp channel for more stories.