Riot police say they have regained control of Pedrinhas prison in Sao Luis, in the state of Maranhao, and are searching the jail for illegal weapons.
Prison guards said fighting had broken out after they discovered inmates digging an escape tunnel.
Brazilian prisons suffer from extreme overcrowding and riots and gang warfare are not unusual.
The riot started after guards received a tip-off that 60 inmates were digging a tunnel through which they were planning to escape at dawn, Maranhao state security secretary Aluisio Mendes said.
“When the guards tried to access the cell in which the entrance to the tunnel was located, the prisoners tried to fight them off,” he added.
Prisoners started a fire and members of rival gangs took advantage of the confusion to settle scores, officials said.
Relatives of inmates gathered outside the prison complex demanding information and threw stones at the guards.
Several buses were also set alight by the relatives, local media reported.
Pedrinhas is notorious for its gang warfare.
Earlier this month, three inmates were killed, one of them decapitated.
Another was electrocuted when he got entangled in cables used to light an escape tunnel last month.
Brazil’s Minister of Justice, Jose Eduardo Cardozo, said last year that Brazil had “a medieval prison system, which not only violates human rights [but] does not allow for the most important element of a penal sanction, which is social reintegration”.
Officials say that despite building new prisons they have not been able to keep pace with the increasing number of detainees, making it hard for them to improve conditions within the jails.