A simple model of a battery is a chemical reaction which produces a constant voltage. But, this chemical reaction takes time. A simple model of the limited speed of that reaction in electrical terms is a series resistance:
simulate this circuit – Schematic created using CircuitLab
When the battery is fresh, R1 is small. As the chemical energy is depleted, R1 gets bigger. Why this happens is complicated, and I'm not a chemist, so I can't tell you in detail, but it has to do with the reactants being used up, and the battery plates getting covered in cruft, and so on.
This resistance, even though it's a combination of electrical and chemical effects, isn't exempt from the laws of physics. It still experiences a loss according to Joule's law:
$$ P = I^2 R $$
This loss of electrical energy must be accompanied by a gain of thermal energy.
If you aren't mixing batteries, then as the batteries become dead, all their resistances rise about the same, so while R goes up, the increasing R\$R\$ also limits the maximum current I\$I\$ that the batteries can supply. Most batteries1 are designed to be safe under any of these conditions.
However, if you mix fresh and dead batteries, then you have the fresh battery which can deliver a large current, into a dead battery which has a high resistance. This results in excessive heat in the dead battery, which may then be damaged or fail, perhaps spectacularly.
1: but certainly not all batteries. Lithium ion batteries, somewhat infamously are not safe when shorted.