Because they mess with you. They screw with you. They also can put the screws to you, torture-wise.
Here is the internet definition as to why.
The slang word screw originated in the Victorian era when a prison warder or officer would give a prisoner a pointless task as a punishment.
Crank Machines One of these punishments was a crank machine used as hard labour. This crank machine would involve the inmate having to turn a handle on a drum which would be filled with sand or water to make it heavier.
The prison guard could tighten a screw in the drum to make turning the handle harder. Prisoners would have to turn the handle a number of times as their punishment. Thus began the slang word screw to mean a prison officer.
There is an example of a crank machine at Stonehaven Tolbooth Museum.