Stage plays were incredibly popular in ancient Greece. It wasn’t uncommon for the problem in the play to be overcome with the help of the/a God like Zeus, who for purposes of the play would be introduced and lowered to the stage via a machine.
So, literally, Deus (god) ex (from) machina (machine).
It has evolved to now describe a trope whereby the primary problem in a story is resolved by an unforeseen, abrupt omnipotent power (relative to the problem).
Latest Answers