In playwriting and fiction, we call it deus ex machina – literally, the “god out of the machine” – which has come to mean a miraculously happy or fortuitous ending to a long and agonizing drama.