ABUJA — Nigerian President Muhammadu Buhari met 107 schoolgirls on Friday freed by militant group Boko Haram and promised to secure the release of their classmates still held in the remote northeast.

Their kidnapping in the town of Dapchi hit Buhari politically by undermining his claims a year before an election to have ended Boko Haram’s nearly decade-long insurgency.

A 2015 military campaign drove the group from most of the territory it controlled but much of the northeast remains beyond government rule and insurgents still stage attacks from strongholds near Lake Chad.

This cheering and hearty development signifies our commitment to the security and well-being of all Nigerians, Buhari said after posing in Abuja for a group photograph with the girls, who spent a month in Boko Haram captivity.

The mass abduction from the girls’ school was an apparent copycat of the 2014 abduction of 270 girls from the town of Chibok.

Boko Haram fighters stunned residents of Dapchi on Wednesday when they drove a line of trucks into the town and released the girls, triggering tears and ululations of joy from relatives.

The fighters, who were shouting God is greatest in Arabic, gave no reason for their change of heart. The government said it had secured their release through back-channel efforts and with the help of some friends of the country.

It said no ransom was paid.

Buhari’s administration agreed to pay millions of euros to secure the release of some of the Chibok girls, as well as ransoms for kidnapped university staff, after negotiations with Boko Haram aided by Switzerland and the Red Cross.

The kidnapping of the Dapchi schoolgirls may have been carried out by a Boko Haram faction allied to Daesh in the hope of securing a similar payout, according to one person briefed on the incident.

The freed girls said they had been treated well but said five of the group had died during their time in captivity and one girl had not been released.

Buhari pledged to continue to work for her freedom, as well as that of the remaining Chibok girls.

While parents of Dapchi girls rejoice because of the reunion with their children, I want to appeal to the Chibok community never to lose hope or despair, he said. We are determined as never before to bring back our remaining Chibok daughters.

Waving the black and white flag used by the Daesh and wearing balaclavas, military fatigues and ammunition belts, members of the group released most of the girls they had abducted in Dapchi early on Wednesday morning.

Witnesses said the militants pulled up near Dapchi police station on Wednesday and shouted that parents should pick up their daughters. Initially, villagers ran away fearing another attack. But when they realised what was happening, they began to cheer and wave at the militants, chasing after their pickup trucks, some recording videos on their phones.

“Dapchi is full of joy,” said Mohammed Mdada, who saw the girls being whipped as they were driven away a month previously. He said the militants apologised to some of the girls’ parents in their language, Kanuri, and shook their hands before driving off.

“They said that if they knew they were Muslim girls they wouldn’t have abducted them,” Mdada said. “They warned the girls that they should stay away from school and swore that if they came back and found any girl in school, they would abduct them again and never give them back.”

One of the goals of Boko Haram – which has kidnapped thousands of girls, boys and women, forcing some of them to blow themselves up, killed thousands of others and displaced millions – is to stop children receiving what it perceives as western-style education.