Gang members from one prison block invaded another part of Altamira jail in Pará state. Sixteen of the dead were decapitated and the remainder suffocated after part of the prison was set on fire, officials said at a news conference.
Two prison officers who were taken hostage have since been freed. The violence began at about 07:00 local time (10:00 GMT) on Monday, and ended at around noon, officials said.
Members of the Comando Classe A (CCA) gang set fire to a cell where rival gang members from Comando Vermelho (Red Command) were kept, the Pará state government said in a statement.