In the late 18th and early 19th centuries, fear of being prematurely buried was never more so widespread, predominately due to the fact that so many people were being buried alive.
This pandemonium led to the invention of the safety coffin, a device designed to provide a person with the tools necessary to alert people above ground that they had been buried alive. By the 1820's there was a great deal of competition to patent the first commercially viable safety coffin design, some designs even included rockets, shovels, food and water.
Most designs never came to fruition and were met with no success. The most commonly designed device was simply a rope connected to a bell which someone would ring if they happened to be buried alive.
This led to the coining of the phrase 'saved by the bell'. Although the phrase is more colloquially associated with and made popular by boxing, its origins can be traced back further than that of its first appearance in boxing in 1893.