A fight between rival gangs in a northern Brazilian prison went on for five hours, officials say.