What is Deus ex Machina?
Deus ex machina is a Latin term that means “God from the machine.”
In ancient Greek drama, dramatists would sometimes use this technique to resolve conflicts. The technique involved using an actual machine (such as a crane or lift) to display the entrance of a god to the stage, who would then solve whatever problem needed solving in the play.
In more modern times, the technique has come to mean anything that happens to miraculously solve character’s problems near the end of the story. It doesn’t necessarily have to be the arrival of a God anymore. We see this in a lot of stories, especially movies, when a character believed to be dead suddenly shows up and kills the bad guy.
See the link below for more detailed information.