The attack near the northeastern city of Maiduguri overnight also wounded dozens of others.
At least 18 people were killed and 84 wounded when Boko Haram militants clashed with Nigerian soldiers near the northeastern city of Maiduguri overnight, the emergency services agency said on Monday.
"So far we have recovered 18 dead bodies from the two villages Bale Shuwa and Bale Kura," on the outskirts of Maiduguri, Benlo Dambatto, an official from the State Emergency Management Agency (SEMA) said.
The gun attack took place on Sunday night in Bale Shuwari, a settlement near army barracks on the edge of Maiduguri's inner city, military sources said.
"The victims were killed while trying to escape the fight between the insurgents and the military." said the Borno branch of SEMA.
It is the most significant attack on the city since the government said last week it was in talks with some of the insurgents.
The government has not disclosed which elements of Boko Haram it was in discussions with, and neither was it clear which faction carried out Sunday's attack.
Maiduguri, the capital of Borno State, is the birthplace of Boko Haram.
The terrorist group has been waging a war against the government for nearly nine years, in a bid to establish a Daesh-style state in the region where Nigeria, Niger and Chad share a border.
The war has claimed at least 20,000 lives and displaced more than two million people.