Rioting inmates beheaded rivals and burned others alive in an explosion of violence in Brazilian jails that left at least 18 people dead, authorities say.