Deus ex machina (Latin: "god from the machine") is a plot device where an insurmountable problem is suddenly resolved by an unexpected, external intervention—often feeling contrived.
In Greek/Roman theater, a crane (machina) lowered an actor playing a god onto the stage to untangle the plot.
Criticized as lazy writing when unearned; but in fantasy/comedy, a well-foreshadowed "divine" twist can delight. Skillful use requires setup so the resolution feels inevitable in hindsight.