An unnamed military officer said on Tuesday that the attack on Monday happened as soldiers were traveling between the towns of Biu and Damboa in the northeastern state of Borno.
"Our men fell into an ambush by Boko Haram terrorists while on their way to Damboa from Biu," the military officer said, adding, "They fought the terrorists and we lost two men in the fight."
A military vehicle was also seized in the assault, he added.
In a separate development on Tuesday, Nigeria's military announced that it had rescued more than 1,100 people, including women and children, who had been held by Boko Haram in different parts of the Lake Chad region near Cameroon.
Operation spokesman Colonel Onyema Nwachukwu said troops killed nearly three dozen Boko Haram militants during raids on Monday in the northern parts of the Lake Chad islands and the Sambisa region. Nwachukwu added that troops also seized a large arms cache.
Boko Haram is still active in its Sambisa Forest enclave in Borno and launches sporadic attacks on civilians and security forces from there.
Tuesday's announcement comes more than a week after Boko Haram abducted over 100 girls from their school in Dapchi in Yobe state.
The latest kidnappings have revived the painful memories of another mass abduction in April 2014, when the militants attacked a girls' school and kidnapped 294 in Chibok, Borno. Many of the girls were released after negotiations, but more than 100 remain in captivity, their whereabouts unknown.
Boko Haram has used kidnapping as a weapon of war, seizing thousands of women and young girls as well as men and boys of fighting age.
The administration of President Muhammadu Buhari is under growing public pressure to respond to the mass abduction in Dapchi. The kidnapping came after repeated claims by the military and government that Boko Haram was on the verge of defeat.
Boko Haram has killed more than 20,000 people and forced two million to flee their homes since the onset of its terror campaign in 2009. It pledged allegiance to the Daesh Takfiri terrorist group in 2015.
The United Nations has warned that areas affected by Boko Haram face a humanitarian crisis.