Poison Cloud and Divine Intervention are the key. Go turn based and shoot it downwards - I found the sweet spot to be roughly around the knuckles of the front foot, but you can do your own experimentation.The only change I would make to Peter2's advice would be to forget melee entirely - the Mega Dragon has something like 10,000 HPs, you're not going to make a dent no matter what you do. You've been hanging onto all those wands/scrolls of Poison Cloud and scrolls of Divine Intervention, haven't you? This is the fight you've been hoarding them for. Good opportunity to reduce your inventory: any character that can't cast Poison Cloud naturally, give them all the scrolls first, then a wand each. Ditto the scrolls of Divine Intervention for those that can't cast it naturally. Have all your characters hitting the Mega Dragon with Poison Cloud, every round, and using Divine Intervention when necessary. Probably takes 5-10 mins of real time with this method to kill the Mega Dragon.
Needless to say, your party needs to be fairly developed anyway, at least enough to survive a single attack without falling unconscious or dying. If you start having to waste turns reviving characters, you'll get into an unrecoverable spiral. You should be able to go anywhere from 1 (if super unlucky) to 10+ rounds without needing Divine Intervention before making your run against the Mega Dragon.
Good luck!