Gunmen believed to be Boko Haram Islamists attacked a secondary school in Nigeria's restive northeast on Saturday, killing 42 people, many of whom were students, a medical worker and residents said Saturday.
But a military spokesman said 20 students and one teacher were killed in the dawn attack at Yobe state.
The attackers rounded up students and staff of the school and placed them in a dormitory before throwing explosives inside and opening fire, said Haliru Aliyu of Potiskum General Hospital, quoting witnesses who escaped.
"We received 42 dead bodies of students and other staff of Government Secondary School (in) Mamudo last night. Some of them had gunshot wounds," Aliyu told AFP, adding that many also had burns.
Aliyu said security personnel were combing the area around the school in search of wounded students who have fled the attack.
"So far six students have been found and are now in the hospital being treated for gunshot wounds," he said.
Mamudo is some five kilometres (three miles) from Potiskum, the commercial hub of the state of Yobe, which has been a flashpoint in the Boko Haram insurgency in recent months.
A local resident who did not want to be named confirmed the attack.
"It was a gory sight. People who went to the hospital and saw the bodies shed tears," he said.
"There were 42 bodies, most of them were students. Some of them had parts of their bodies blown off and badly burnt while others had gunshot wounds."
He said the attack was believed to be a reprisal carried out by Boko Haram for the killing of 22 of the Islamist group's members during a military raid in the town of Dogon Kuka on Thursday.
A senior police officer said the students were asleep when the attackers stormed their school.
They then started "shooting sporadically and subsequently set the students' hostel ablaze".
Lieutenant Eli Lazarus, spokesman for the joint military task force in the state, said the gunmen "stormed the school around 5:30 am and began to shoot at the students from different directions".
He however, gave a different toll for the attack.
"20 students and a teacher were killed, four were critically injured in the attack and are now receiving treatment," he said.
The military often underplays casualty figures in Nigeria.
Residents said the latest incident has sparked panic among students in the area and many of them have left their dormitories for home even though schools are still in session in the state.
Nigeria declared a state of emergency in three flashpoint states -- Yobe as well as Adamawa and Borno -- when it launched a major offensive against Boko Haram on May 15.
Mobile phone lines have been cut in much of the northeast since the offensive, and access to the area is limited.
Boko Haram, which means "Western education is sin", has killed hundreds of students in attacks on schools in the region in recent months.
On June 17, Boko Haram extremists shot dead nine students as they sat an exam in a private school in Maiduguri, a stronghold of the group.
A day earlier, gunmen opened fire on a secondary school in Damaturu, the capital of neighbouring Yobe state, killing seven students and two teachers. Two of the attackers were also killed, the army said.
Violence linked to the Boko Haram insurgency has left some 3,600 people dead since 2009, including killings by security forces, who have come under major criticism for alleged abuses.