Horatio is approached by sailors who give him a letter from Hamlet.
I have a wild conjecture: Hamlet arranged this. (You AP students need to be clear: this is my own musing. I would not include it on a test essay, as it is not supported by clear and copious text evidence.)
Inference: if “I see a cherub that sees [Claudius’ purpose]” (4.4.56) means that Hamlet has figured out that he’s being sent to his death— further supported by “There’s letters sealed; and my two schoolfellows, whom I trust as I will adders fanged,/…they must sweep my way/ and marshal me to knavery” (3.4.225-228)—and if Hamlet is resourceful enough to know a variety of rough characters, it would be quite convenient to arrange a pirate encounter that lasts only long enough for him to board the pirate ship and be “held hostage” and, thus, escape.
Total conjecture on my part.