GENEVA (Reuters) - The United Nations has negotiated the release of nearly 900 children detained by Nigeria’s army and security forces after they have retaken land from Boko Haram militants, a senior official said.

The 876 children had been held in the barracks in Maiduguri, UNICEF’s Manuel Fontaine said after visiting the northeastern city.

It was not immediately clear how long they had been held, but the army routinely detains civilians who have been living in areas that had been ruled by the insurgents on suspicion that they too might be linked to militant activities.

However, rights groups say there is no proper legal process for such civilians, including the children, since they do not get formally charged and some end up in so-called rehabilitation centers, which the groups say are like prisons.

The United Nations says children should not be detained.

“We fear that there are still kids who are being at least temporarily detained because they are being released from Boko Haram areas by the army but then kept for a while,” Fontaine, UNICEF’S regional director for Western and Central Africa, told reporters by telephone.

He give no details of the ages of the children or how long they had been at the barracks - but after President Muhammadu Buhari came to power in May 2015, security forces began an offensive, backed by neighbouring countries, to retake territory from Boko Haram, meaning at least some of the children could have been held for a year or more.

There was no immediate comment from the army. Army officials say they need to question civilians to establish whether they have any ties with the militant group, which has been trying for seven years to set up an Islamic state.

The security situation remains volatile in northeastern Borno state and its capital Maiduguri because Boko Haram still stages suicide bombings, often using women or teenagers.

Fontaine also said the conflict, which has killed thousands and displaced more than 2 million, had separated around 20,000 children from their parents, of which 5,000 had since been reunited with their families.

“Once we get children out, there is a major issue of stigmatization in the communities,” Fontaine said. “There is a sense that children who have been associated with Boko Haram for a while, could be, and in some cases we have some evidence, are rejected by community and people around them.”

This was also a problem for the girls freed from the town of Chibok, he said. Nigeria this month negotiated, with the help of Switzerland, the release of 21 of more than 200 girls Boko Haram kidnapped in April 2014.