Screw (in this particular slang sense) appeared in the mid 18th century to mean a false or skeleton key. Perhaps because prison officers carry many keys around with them, the word became attached to them over the next fifty years or so. As a result, the earliest-recorded reference in print for screw = warder dates from 1812. There is also a suggestion that the 'screw' was a name given to a mediaeval torture device, which would also have been used in prisons or even to a sort of treadmill that prisoners had to 'work' on.