It depends upon the jail.
If you mean the pillory, where the tramp throws tomatoes at you, you must match the lock with the correct key.
If you mean Evil Bob's jail, you need to pull the lever on the wall, then pop the matching balloon animal.
If you mean the Port Sarim jail, you must pick the lock on the door.
| Learn more about this on|
the RuneScape Wiki.
See Random events.