ISLAMIC militants have attacked a boarding school in northeast Nigeria, killing 42 people, mostly students, some of the pupils being burned alive.

Gunmen believed to be Islamists from Nigeria's Boko Haram insurgent group, whose name means "Western education is sacrilege", committed the overnight attack on a secondary school in restive Yobe state, a medical worker and residents said Saturday.

"We received 42 dead bodies of students and other staff of Government Secondary School (in) Mamudo last night. Some of them had gunshot wounds while many of them had burns and ruptured tissues,'' Haliru Aliyu of the Potiskum General Hospital told AFP.

Mamudo is some five kilometres from Potiskum, the commercial hub of Yobe State which has been a flashpoint in the Boko Haram insurgency in recent months.

Parents screamed in anguish as they tried to identify charred bodies and gunshot victims.

Farmer Malam Abdullahi found the bodies of two of his sons, a 10-year-old shot in the back as he apparently tried to run away, and a 12-year-old shot in the chest.

"That's it, I'm taking my other boys out of school," he said as he wept over the two corpses. He said he had three younger children in a nearby school.

"It's not safe," he said. "The gunmen are attacking schools and there is no protection for students despite all the soldiers."

"We were sleeping when we heard gunshots. When I woke up, someone was pointing a gun at me," said 15-year-old Musa Hassan.

He put his arm up in defence, and suffered a gunshot that blew off all four fingers on his right hand, the one he uses to write with.

He said the gunmen came armed with jerry cans of fuel that they used to torch the school's administrative block and one of the hostels.

"They burned the children alive," he said, the horror showing in his wide eyes.

He and teachers at the morgue said dozens of children from the 1200-student school escaped into the bush but have not been seen since.

Some bodies are so charred they could not be identified, so many parents do not know if their children survived or died.

Islamic militants from Boko Haram and breakaway groups have killed more than 1600 civilians in suicide bombings and other attacks since 2010, according to an Associated Press count.

Scores of schools have been burned down in the past year in northeast Nigeria.

President Goodluck Jonathan declared a state of emergency May 14, and deployed thousands of troops to halt the insurgency, acknowledging that militants had taken control of some towns and villages.

The military has claimed success in regaining control of the area - the states of Adamawa, Borno and Yobe . However, the area covers some 155,000 square kilometres, or one-sixth of the sprawling country.

Soldiers say they have killed and arrested hundreds of fighters.

But the crackdown, including attacks with fighter jets and helicopter gunships on militant camps, appears to have driven the extremists into rocky mountains with caves, from which they emerge to attack schools and markets.

The militants have increasingly targeted civilians, including health workers on vaccination campaigns, teachers and government workers.

Farmers have been driven from their land by the extremists and by military roadblocks, raising the specter of a food shortage to add to the woes of a people already hampered by the military's shutdown of cell phone service and ban on using satellite telephones.