Deus ex machina
Deus ex machina (Latin: [ˈdeʊs ɛks ˈmaː.kʰɪ.naː]: or ; plural: dei ex machina) is a Latin calque from Greek ἀπὸ μηχανῆς θεός (apò mēkhanês theós), meaning "god from the machine". The term has evolved to mean a plot device whereby a seemingly unsolvable problem is suddenly and abruptly resolved by the contrived and unexpected intervention of some new event, character, ability or object. Depending on how it is done, it can be intended to move the story forward when the writer has "painted himself into a corner" and sees no other way out, to surprise the audience, to bring the tale to a happy ending, or as a comedic device.
Origin of the expression
The term was coined from the conventions of Greek tragedy, where a machine is used to bring actors playing gods onto the stage. The machine could be either a crane (mechane) used to lower actors from above or a riser that brought actors up through a trapdoor. Preparation to pick up the actors was done behind the skene. The idea was introduced by Aeschylus and was used often to resolve the conflict and conclude the drama. Although the device is associated mostly with Greek tragedy, it also appeared in comedies.