Although it seems unlikely to us, Hamlet is returned to Denmark by pirates who apparently attack the ship he is on. In a letter to Horatio that is delivered to sailors, Hamlet tells his friend the story of this pirate tale.
When the pirate ship sails alongside Hamlet's ship, he boards the pirate ship to escape. The pirates agree to help Hamlet; in return he is to do them "a good turn," or favor. He doesn't, however, explain what that favor is. Perhaps we can assume that since he is a prince, the pirates ask for safe passage or maybe even money if they deliver him successfully to Denmark.