Royally Screwed

Getting screwed - in the sense of being at the receiving end of a whole bunch of badness - has its origins in carpentry.  It's nothing to do with thumbscrews.  It has everything to do with keeping a thing under pressure - or releasing it.  Things can get literally screwed up or screwed down.

If you are trying to clamp something with screws you are screwing something down.  If you have a screw loose then your efforts will result in a screw-up.  A screw-up in a steam locomotive shop may result in a most public screw-up somewhere along the line.