Northwest and central Nigeria have for years been plagued by gangs of criminals, known as bandits, who raid villages, kill and abduct residents for ransom and loot and burn homes.
Though driven by financial gain, there are concerns among officials and analysts about their growing ties with jihadists waging a 15-year insurgency in the northeast.
Troops on "a fighting patrol" in Shiroro district in Niger state were ambushed on Friday, leading to a gunfight, the military said in a statement late on Sunday.
"The troops suffered a temporary setback as six personnel comprising two officers and four soldiers paid the supreme price," army spokesman General Onyema Nwachukwu said in the statement.
Two other soldiers were wounded in the ambush, the statement said.
"Troops gallantly fought through the ambush and eliminated several of the terrorists as well as captured some of their equipment," it added.
The army spokesman said the ambush would be avenged by the troops who were in pursuit of the bandits that fled after the gunfight.
Shiroro district is a known enclave of bandit leader Dogo Gide, who has forged an alliance with Islamic State-linked jihadists.
In August, Gide's faction killed 23 soldiers and three local vigilantes in an ambush on a security convoy on a highway in Niger state, according to military sources.
A military helicopter sent to evacuate the casualties crashed, killing all onboard, with the bandits claiming to have shot it down.