About 40 migrants fought police at the centre in the Mediterranean port of Valencia late on Sunday and dozens tried to escape, a police spokesman said.
"They jostled police, hit them, ripped the keys from one of them and removed the belt from another. Five police officers were lightly injured," he said.
About 20 migrants, all of them men, used the keys to reach the roof of the centre and tried to escape using sheets and blankets and threw rocks at police who tried to stop them.
Migrants inside the centre smashed furniture and tried to set fire to mattresses, prompting firefighters to intervene. Migrants also threw rocks at the firefighters.
The centre, dubbed an Internment Centre for Foreigners or CIE by its Spanish acronym, is one of several used to hold undocumented immigrants while their case is analysed or until they can be deported.
The spokesman could not say how many migrants were at the centre at the time of the riot, saying only that it had an occupancy rate of 42 percent.
Non-governmental organisation "CIEs NO", which lobbies against holding undocumented migrants in detention centres, said the centre has 150 places.