Ketubot 30 - 36
The Secrets of Life and Death
Four categories of capital punishment existed when the Sanhedrin had the power to inflict the death penalty on offenders, each of them suited to the nature of the sin. Even after capital punishment was no longer practiced Heaven saw to it that those committing grave sins died violent deaths in the manner similar to the sort of execution that would have been carried out on them in the days of the Sanhedrin.
How is it then, asks Tosefot, that we see idol worshippers and other serious offenders dying natural deaths.
The answer given is that perhaps unknown to us those sinners repented and were forgiven or at least had their sentence lightened. Tosefot adds that there may even be a possibility of a particular merit that the sinner had which spared him from a violent death and entitled him to have his retribution postponed for the afterlife.
Maharsha extends this last point to explain why we sometimes find righteous people like Rabbi Akiva dying violent deaths. In such cases the reverse is true. These saintly people are granted an opportunity in their lifetime here on earth to achieve atonement for any sin they have committed so that they can enter the afterlife with a clear record.