"Saved by the bell" most likely comes from a boxing term where a bell rings between the rounds. The boxer getting beaten could be saved from getting knocked out by the bell ringing (or something like that). http://phrases.org.uk/meanings/saved-by-the-bell.htm
When growing up, the phrase seems appropriate for school here, because a bell rings between classes. So you'd be saved from more of the class or more school by a bell. There was even a kids show a while back called "Saved by the Bell". I might be showing my age a bit with that comment.
Yet again it translates almost literally to Italian which has the saying "Salvato dalla campanella". Interestingly enough, it seems at least according to this blog (http://panda-time.blogfree.net/?t=3463906) that the Italian saying comes from the practice of tieing a bell to the hands of dead people as they got buried, so that they might ring it and get help in case they weren't actually dead. But I must admit that the boxe explanation is way less macabre.
My understanding is that the English phrase comes from a part of English history in which they found people were sometimes buried alive (they found scratch marks on the insides of the coffins). The solution was to attach a string to the arm of the buried person, which was attached to a bell on the surface. If the buried person was alive and started moving, they would be a "dead ringer" (another English expression), and a person who's job it was to be looking after the graveyard, would have then been able to dig them out - they'd been "saved by the bell". The person who had the long overnight shift, waiting in case a bell would ring so that they could dig up a buried, living body, had "the graveyard shift" (an expression for a late night appointment of work).
I'm Brazilian native and I've never heard that expression in my life. So, I decided to search for the meaning and I found that site: https://www.dicionariopopular.com/salvo-pelo-gongo/ .