The attack also left a female school teacher and an eight-year-old schoolgirl with injuries.
The man, identified as Gaji Adamu, entered Jafi Primary School, Kwaya Kusar town around 9:30am and carried out the attack that claimed the lives of the pupils, both boys aged about five and seven.
The attack also left a school teacher, identified as Ms Salamatu, and an eight-year-old schoolgirl with injuries.
According to a report by AFP, the teacher managed to raise the alarm after she was struck in the hands and the attacker was overpowered.
The state's Police Public Relations Officer (PPRO), Victor Isuku, confirmed the attack in Maiduguri later on Thursday and disclosed that the attacker was in custody under armed guard in a hospital after he was beaten by the mob that caught him. He also disclosed that the man is suspected to be mentally-unstable.
He said, "Today Thursday 30/11/17, at about 0930 hours, one Gaji Adamu, male of Kubu village of Kwaya Kusar Local Government Area, suspected to be of unsound mind, armed with machete entered into Jafi Primary School, Kwaya Kusar town and macheted two male pupils to death, while a teacher and a female pupil were wounded in the attack.
"As a result, irate youths mobbed the suspect, who is now unconscious and admitted in hospital under protective custody. The corpses and injured have been evacuated to Gombe General Hospital."
Despite the official toll of only two deaths, an eyewitness of the attack reported that the eight-year-old schoolgirl also died on her way to the hospital.
According to a report by the News Agency of Nigeria (NAN), Malam Abdulhamid, a resident of the area, said that three pupils died as result of injuries sustained in the attack.
He said, "It is not clear how the assailant managed to infiltrate the school. He attacked pupils with a machete killing three of them and caused fatal injuries to others.
"A female teacher, Ms Salamatu, was also injured when she confronted the assailant in an attempt to save the children from the brutal attack.
"Some of the pupils sustained injuries while trying to escape. People came to their rescue and arrested the attacker."
The state's Commissioner of Police, Damian Chukwu, has directed for the transfer of the case to the Criminal Investigation and Intelligence Department (CIID) for investigation.