A deus ex machina ( or ; Latin: "god from the machine" ; plural: dei ex machina) is a plot device whereby a seemingly unsolvable problem is suddenly and abruptly resolved, with the contrived and unexpected intervention of some new event, character, ability, or object. Depending on how it's 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, or to bring a happy ending into the tale.
Linguistic considerations
The Latin phrase deus ex machina comes to English usage from Horace's Ars Poet...