More than 300 schoolboys have been released after being kidnapped and held for days by armed bandits in a forest in north-west Nigeria, the government has said.

Their release on Thursday night came hours after a grainy video allegedly showing some of the schoolboys being purportedly released by the militant Islamist group Boko Haram, which had claimed credit for a kidnapping that left Africa’s most populous country reeling.

President Muhammadu Buhari welcomed the release in a statement and acknowledged that his government had work to do to improve security in Nigeria, where massacres and mass abductions have become increasingly common.

“Our administration is fully aware of the responsibility we have to protect the lives and property of all Nigerians,” he said. “I ask Nigerians to be patient and fair to us as we deal with the challenges of security, the economy, and corruption. We will not relent.”

The brazen attack on the school is yet another blow to the record of Mr Buhari, a former general elected in 2015 on a national security pledge. If Boko Haram is confirmed to have been involved, it would mark a sharp escalation in its activity and a wide expansion of its sphere of influence.

Authorities have said the attack last week was carried out by well-known local bandits affiliated with the Islamists. It is not clear how the schoolboys’ release was negotiated or whether any ransom was paid.

Any connection between the Islamist group that has largely been confined to north-east Nigeria for years and the gangs of AK-47-wielding criminals who have killed people in the north-west sets a dangerous precedent, said Bulama Bukarti, an analyst at the Tony Blair Institute for Global Change.

“This incident is a sad demonstration that Boko Haram is expanding more than ever before,” said Mr Bukarti, who has long warned of growing links. “Boko Haram will provide criminals that have been horribly shedding blood for ages religious justification and thus make them more vicious and allow them to recruit more young people.”

Boko Haram leader Abubakar Shekau claimed credit for the attack on the school in a four-minute audio message on Tuesday. He said the group had conducted the attack because “western education is not the type of education permitted by Allah and his holy Prophet”.

Boko Haram translates to “western education is forbidden”.

The incident came six years after Mr Shekau made global headlines for kidnapping 276 schoolgirls in Chibok, launching the viral #BringBackOurGirls hashtag that drew support from the likes of Michelle Obama. Some escaped and were rescued, others released for more than €3m in ransom. More than 100 of those girls have never been heard from, and are presumed either dead or forcibly married to group members.

After pledging allegiance to Isis in 2014, Mr Shekau broke from the group over ideological differences in 2016. But analysts say that as the remainder, known as Islamic State West Africa Province (Iswap), has gained prominence in the north-east, Mr Shekau and Boko Haram have sought to further cultivate ties with the bandit groups operating in the north-west.

“Bandits” is a catch-all term in Nigeria for non-ideological gangs of motorcycle-driving men who rove the countryside ransacking villages and abducting busloads of people on major highways. It affects nearly every corner of the country, but its heart is the north-west, where deaths rival those in the north-east, at more than 2,000 this year, according to data collected by José Luengo-Cabrera, a security analyst.

The attack came just weeks after Boko Haram, which operates largely from the Lake Chad basin, killed scores of rice farmers in north-east Nigeria, and days after the group killed 28 people and burned 800 homes in a village just over the border in Niger.

Analysts warn that Boko Haram and Iswap could connect with the jihadis terrorising Mali, Niger and Burkina Faso, who, working with ethnic militias and criminal elements, have killed thousands and displaced millions.

“The northwest is the area standing between the Sahel and Lake Chad region,” Mr Bukarti said. “Boko Haram’s consolidation in the northwest will provide a bridge between jihadis in the two epicenters, making movement of fighters, weapons and loot easier.”