Deus ex machina
Latin, meaning "god out of the machine." The expression comes from ancient Greek drama, where the plot device would be the character of a deity, lowered with a machine onto the stage to intervene in human affairs.
Nowadays the expression is employed more figuratively, for any device used to bring about a magical resolution of the plot (or plot twist), although in SF authors who place an emphasis on technology sometimes equate machines with deities.
Deus ex machina endings.