Why is the story of Troy and the Trojan horse contained in Hamlet’s recitation to the players?the question is in act 2 scene 2.
In addition to, or perhaps as enrichment of, cybil's answer above, we might add that Hamlet is filled to the brim with sons avenging the murder of their fathers. The play begins with discussion in Act I, scene 1, about why Denmark is preparing for war. And the reason given is in part that "Young Fortinbras" is out to avenge the death in combat of his father who died at the hands of Hamlet's father. Later we can contrast the urgent forcefulness of Laertes in his desire for instant revenge against whomever is responsible for the death of his father Polonius. Initially, Laertes reenters the court thinking Claudias is to blame in Act IV, scene 5, until Claudias convinces him that Hamlet was responsible, which is true enough. But what matters is the urgency, the need for action of these other sons. For instance, Laertes tells the Queen, when she urges him to be calm over the death of his father: "That drop of blood that's calm proclaims me bastard, Cries cuckold to my father, brands the harlot Even here between the chaste unsmirched brow Of my true mother" (IV.v.117). In more conventional language: If I am not filled with rage at this outrage [if I try to be calm] then I am NOT my father's true son; ergo, I must be a bastard [someone else's son] and my father must be a cuckold [a man whose wife cheated on him, also a fool] and my mother must be a harlot, whore, unfaithful wife--which she is not!
And as discussed earlier, we now add in the Play within the Play, yet another son who takes direct action is seeking revenge for the death of his father. The point of course is to contrast Hamlet's inaction, often called the problem of Hamlet, with the more forceful actions of these other characters.
The theme of deception is an important one in Hamlet. The prince is already pretending to be mad in the hope that he can gather information to verify the Ghost's claim that Claudius murdered him. The reference to the Trojan horse underscores the theme of deception as well as the theme of revenge, which is another important element in the play. In The Iliad, Pyrrhus is hiding in the Trojan horse so that he can avenge the death of his father Achilles. Hamlet is symbolically hiding in his guise of madness so that he can avenge the death of his father as well.