How does Hamlet escape from the ship?
Hamlet is on board a ship bound for England. At some point during the voyage the ship is attacked by pirates. By this time, Hamlet has read the letter from Claudius to the king of England requesting his assassination. He's cunningly changed the contents of the letter so that his old school friends end up being killed instead of him. So, Hamlet sees the attack by pirates not as a calamity, but as a great opportunity to further his dastardly ploy. After a brief skirmish between the ship's crew and the pirates, he boards the pirate ship, and as a royal prisoner is treated very well by his captors:
"They have dealt with me like thieves of mercy, but they knew what they did; I am to do a good turn for them." (act 4 scene 6)
The pirates have a very valuable prisoner on their hands. It's clear that they'll want something equally of value in return for letting him go.
As Hamlet is on his voyage by ship to England, his ship is attacked by a pirate ship. In the commotion of the ensuing impromptu battle, Hamlet ends up boarding the pirate ship. Once the battle has culminated, the pirates decide they will drop Hamlet off back in Denmark. Hamlet communicates all this to Horatio in a letter.
Also, Hamlet describes the pirates on the ship as "merciful," and the reader gets the sense that the pirates have agreed to let Hamlet go free with the suggestion that Hamlet is going to do some favor for them (after all, he is the prince of Denmark, so he presumably has some political pull!)
After a fierce fight, Hamlet manages to jump onto the pirates' ship who promise to take him back to Denmark in exchange of a favour Hamlet does not mention in the letter sent to Horatio. This is a time when the audience are to suspend their belief and ignore the fact that the jump is too much exaggerated and overlook the fact that Hamlet is actually helping pirates let alone calls them 'thieves of mercy'.