Nearly 500 men and boys have been rescued from a building in the northern city of Kaduna, where the detainees were allegedly sexually abused and tortured, Nigerian police said.
Children as young as five were among those in chains at what was thought to be an Islamic school, officers said.
Kaduna police chief Ali Janga told the BBC the building was raided after a tip-off about suspicious activity.
He described it as a "house of torture" and a place of human slavery.
Eight suspects, most of them teachers, were arrested. The police chief said the detainees - some with injuries and starved of food - were overjoyed to be freed.
The detainees said they had been tortured, sexually abused, starved and prevented from leaving - in some cases for several years.
"I have spent three months here with chains on my legs," Bell Hamza reportedly told Nigerian media.
"This is supposed to be an Islamic centre, but trying to run away from here attracts severe punishment; they tie people and hang them to the ceiling for that."
Some of the children told police that their relatives had taken them there, believing the building to be a Koranic school.
Two of the children freed by police said their parents had sent them from Burkina Faso. Police believe the rest are mostly from northern Nigeria.
Islamic schools are popular in the region but there have long been allegations of abuse in some schools, and of pupils forced to beg for money on the streets.
One parent told Reuters news agency that they did not know their children would face "this kind of harsh condition".
The captives are sheltering at a camp where their families are arriving to identify them. Hafsat Muhammad Baba of the Kaduna state government told the BBC the government will continue to provide medical care for the men and boys.