The phrase “deus ex machina” refers to a person or thing that appears unexpectedly in fiction to solve a horribly complicated plot.
The words “deus ex machina” actually mean “a god from a machine” and it refers to the physical crane that held a “god” above the stage of plays. Apparently in the 5th century B.C it wasn’t uncommon to just drop a god down from the sky in the last scene to fix up the overly contrived plot.
Thanks to Merriam-Webster Dictionary