deus ex machina, a person or thing that appears or is introduced into a situation suddenly and unexpectedly and provides an artificial or contrived solution to an apparently insoluble difficulty.
The term was first used in ancient Greek and Roman drama, where it meant the timely appearance of a god to unravel and resolve the plot. The deus ex machina was named for the convention of the god’s appearing in the sky, an effect achieved by means of a crane (Greek: mēchanē). The dramatic device dates from the 5th century bc; a god appears in Sophocles’ Philoctetes and in most of the plays of Euripides to solve a crisis by divine intervention.
Since ancient times, the phrase has also been applied to an unexpected saviour or to an improbable event that brings order out of chaos (e.g., the arrival, in time to avert tragedy, of the U.S. cavalry in a western film).