Hundreds of students are feared missing after gunmen raided a secondary school in Katsina, home state of president Buhari.
The attackers arrived on motorbikes and started shooting into the air, causing people to flee, witnesses said.
They targeted the Government Science Secondary School – where more than 800 students are said to reside – in Katsina state on Friday evening.
On Saturday, the military said it had located the gunmen’s hideout in a forest and exchanged gunfire with them.
The outcome was unclear but officials said there were no reports of students being injured.
Meanwhile, President Buhari condemned the attack and ordered the school to carry out a full audit of students to find out how many are missing. Parents who dashed to the school to take their children home were also asked to notify the school authorities.
Residents living near the all-boys boarding school in the Kankara area told the BBC they heard gunfire at about 23:00 (22:00 GMT) on Friday, and that the attack lasted for more than an hour.
Security personnel at the school managed to repel some of the attackers before police reinforcements arrived, officials said.
In a statement on Saturday, police said that during an exchange of fire, some of the gunmen were forced to retreat. Students were able to scale the fence of the school and run to safety, they said.
About 200 students who had fled – and were initially deemed missing – later returned. However, witnesses said they saw a number of students being taken away by the gunmen.