About 40 people were on the roof shouting "freedom, freedom" in Spanish and waving a banner in protest at their detention conditions, an AFP photographer said.
Police said the migrants, who were unarmed, had spent the night on the roof after starting the riot late Tuesday at the centre in Madrid's southern district of Aluche.
"We are trying to negotiate with them... We'll see if they come down by themselves," said the national police spokesman in Madrid.
"Police are in control of the situation."
By mid-morning on Wednesday they had voluntarily come down from the top of the centre, police said.
A spokeswoman for the city hall said no one had been injured in the mutiny.
Police said the protesters had broken furniture to make their way to the roof, while Spanish daily El Pais reported that some had blocked security cameras inside the building.
Madrid mayor Manuela Carmena said late Tuesday that she was ready to attempt a mediation if requested by the district authorities.
"Events at the detention centre continue to worry me," she said on Twitter. "Human rights are the priority."
Associations and academics have often denounced prison-like conditions in Spain's seven immigration detention centres -- some of which have overcrowded rooms, dirty toilets and little in the way of social services or translators.
The centres are meant for people who have come to Spain without a residence permit and are in the process of being deported.
Madrid councillor Javier Barbero, who is in charge of health and safety, described the centres as an "institutional failure".
Early this month, 67 migrants without residence permits managed to flee a detention centre where they were being held near the southeastern city of Murcia. One of the migrants first pretended to be ill, before the others rose up in mutiny as the ambulance arrived.