Around 1 pm on Tuesday, a group of militants, who found themselves trapped at an army checkpoint on a road leading to the city of Biu, hurled a grenade into the crowd waiting to pass the road.
The soldiers then opened fire on the attackers' vehicle, killing 17 of the militants.
Civilians and government troops made up the rest of the fatalities, security sources told.
Five days ago, a woman set herself on fire at a market in Biu, resulting in the deaths of six persons.
More than 60 people have died over the past seven days in attacks by Boko Haram militants in the country and the neighbouring nations of Niger, Chad and Cameroon.
The latest round of attacks comes as a multinational force jointly led by Nigeria and Chad is trying to drive Boko Haram from its strongholds ahead of the March 28 Nigerian Presidential election.