Deus ex machina

The phrase deus ex machina (literally "god out of a machine") describes an unexpected, artificial, or improbable character, device, or event introduced suddenly in a work of fiction or drama to resolve a situation or untangle a plot (e.g. the rope that binds the hero's hands is luckily chewed off by a rat, or an angel suddenly appearing to solve problems).

Linguistic considerations
The Latin phrase "deus ex machina" has its origins in the conventions of Greek tragedy. It refers to situations in which a mechane (crane) was used to lower actors playing a god or gods onto the stage. Though the phrase is accurately translated as "God from a machine," in literary criticism, it is more commonly translated to "God on a machine." The machine referred to in the phrase is the crane employed in the task. It is a calque from the Greek  ápo mēchanēs theós'', (pronounced in Ancient Greek ).

The Greek tragedian Euripides is notorious for using this plot device as a means to resolve a hopeless situation. For example, in Euripides' play Alcestis, the eponymous heroine agrees to give up her own life to Death in exchange for him sparing the life of her husband, Admetus. In doing so, however, she imposes upon him a series of extreme promises. Admetus is torn between choosing death or choosing to obey these unreasonable restrictions. In the end, though, Heracles shows up and seizes Alcestis from Death, restoring her to life and freeing Admetus from the promises. The first person known to have criticized the device was Aristotle in his Poetics, where he argues that a good tragedy must remain plausible.

Ancient Roman dramatists continued the use of the device.

Modern uses
The phrase has been extended to refer to any resolution to a story that does not pay due regard to the story's internal logic and is so unlikely that it challenges suspension of disbelief, allowing the author to conclude the story with an unlikely, though more palatable, ending.

In modern terms the deus ex machina has also come to describe a being, object or event that suddenly appears and solves a seemingly insoluble difficulty, where the author has "painted the characters into a corner" that they can't easily be extricated from (e.g. the cavalry unexpectedly coming to the rescue, or James Bond using a gadget that just so happens to be perfectly suited to the needs of the situation).

Other examples are seen in Dante Alighieri's Inferno when a mysterious personage (variously identified) "sent from Heaven" clears the path of fallen angels and opens the gates of Dis for Dante and Virgil to pass; and in Michael Crichton's The Andromeda Strain, where the main adversary in the novel is rendered harmless by random mutation. The device is a type of twist ending. A recent example of this occurs in the film adaptation of Dan Brown's novel "The Da Vinci Code" (directed by Ron Howard for the big screen) where a bird is seen to disturb the killer just before he shoots the lead character, thus giving him the opportunity to flee.

The notion of deus ex machina can also be applied to a revelation within a story that causes seemingly unrelated sequences of events to be joined together. Thus the unexpected and timely intervention is aimed at the meaning of the story rather than a physical event in the plot. This may more accurately be described as a plot twist.