When you have an RC circuit with a battery and the capacitor is charged, the capacitor has a store of energy in the electric field equal to $\frac 12 CV^2$.
When the battery is disconnected that energy stored in the capacitor becomes heat as a current is passed through the resistor.
The current is generated because there is a potential difference across the plates of the capacitor.
The current decreases to zero when there is no longer an electric field ie the capacitor is totally discharged.
There is a parallel to this when you consider an inductor in that there is energy stored in the magnetic magnetic field produced by the inductor equal to $\frac 12 LI^2$.
When the battery is disconnected the current cannot collapse to zero instantaneously because that would imply that the magnetic flux linked with the capacitor would go to zero instantaneously and thus produce an infinite induced emf (Faraday).
So when you disconnect the battery there is still a current flowing in the circuit but that current is decreasing and thus there is an induced emf in opposition to the flux through the inductor decreasing.
As the current is decreasing the energy dissipated stored in the magnetic field (which depends on the current) is changed into heat in the resistor.
This continues until the magnetic field is no more when the current is zero.