Fighters loyal to long-time factional leader Abubakar Shekau launched the attack on Sunday evening some 15 kilometers (nearly 10 miles) from the town of Banki.
Nigeria's President Muhammadu Buhari came to power on a pledge to defeat the extremist militants but despite his repeated claims that they have been weakened to the point of defeat, attacks persist.
His failure to end the violence is again a polling issue as Buhari seeks re-election this weekend, Presstv reported.
"We lost two soldiers to Boko Haram terrorists who attacked Alpha Company location," a military officer in Banki said on condition of anonymity.
"Six soldiers were also injured in the attack," he said. The account was backed by a civilian militia member in the town.
The injured troops were evacuated to a hospital in Mora, across the border in Cameroon.
Banki is 130 kilometers (80 miles) southeast of Maiduguri, the Borno state capital, and houses some 45,000 people displaced by the conflict in a sprawling camp.
Boko Haram has launched several attacks in and around Banki, targeting troops and the displaced.
Last month, two soldiers were killed and seven civilians injured in an ambush on a convoy of traders under military escort in Chachile village.
Boko Haram has in recent months stepped up attacks against military targets.
The militancy, which began in 2009, has killed more than 27,000 people and left 1.8 million homeless in northeast Nigeria.
The violence has spilled into neighboring Niger, Chad and Cameroon.