In act 2, scene 2 of William Shakespeare's Hamlet, Hamlet decides that the best way to test the truth of the ghost of his father's accusation against Hamlet's uncle Claudius—that Claudius murdered Hamlet's father— is to present a play and observe Claudius's reaction to it. Hamlet has heard that guilty people watching a play have been prompted to confess their wrongdoing (2.2.583–587). Hamlet believes that the play best suited to produce this effect in Claudius is an old Italian play called The Murder of Gonzago, and he asks the actors who have recently come to Elsinore Castle to perform the play for the court (2.2.530–531). This play is known as the "play-within-a-play" in Hamlet.
Shakespeare's play-within-a-play of The Murder of Gonzago is based in part on a court document which describes the murder of Francesco Maria I della Rovere, Duke of Urbino, Italy, which occurred in 1538. The document also contains a letter written by Luigi Gonzago, who was the Duke's barber when the Duke was in Mantua, in which Gonzago protests his innocence of the murder of the Duke.
An actual play of The Murder of Gonzago which Hamlet asks the players to perform at court doesn't exist. Shakespeare simply devised The Murder of Gonzago based on the court document's description of the murder of Francesco Maria I della Rovere, and inserted the play into act 3, scene 2 of Hamlet.
There are a number of parallels between the Duke's death in the court document and the play-within-a-play which Shakespeare titled The Murder of Gonzago, and between the court document and the murder of Hamlet's father.
For example, the murderer in both plays is closely related to the victim. Duke Francesco Maria I della Rovere was murdered by his wife's cousin, Luigi Gonzago, and Hamlet's father was murdered by his own brother, Claudius. Shakespeare changes the name of the Player King in the play-within-a-play from della Rovere to Gonzago:
HAMLET. Gonzago is the Duke's
name; his wife, Baptista. (3.2.228–229)
In the play-within-a-play, the Player King says that he and the Player Queen have been married for thirty years (3.2.144-149), which is the same length of time that Duke Francesco della Rovere was married to his wife when he was murdered, and about the same length of time that Hamlet's father and mother, Gertrude, were married, according to the Gravedigger (5.1.140, 143–144).
Hamlet's father was a famous soldier, as was Duke Francesco della Rovere, and they both had many victories. Notable among old Hamlet's victories is his defeat of King Fortinbras of Norway. This is the reason that Young Fortinbras is menacing Denmark at the beginning of Hamlet (1.2.17-25), and trying to regain lands that his father lost to old Hamlet.
Shakespeare might also have been familiar with a portrait of Duke Francesco della Rovere painted by Titian just before the Duke's death. The Duke—full-bearded and looking fierce, if not a little world-weary—is portrayed in full armor. In act 1, scene 2, Horatio describes the ghost to Hamlet much as the Duke appears in the painting:
HORATIO. A figure like your father,
Armed at point exactly, cap-à-pie [meaning head to foot]. (1.2.207-208)
According to Horatio, the ghost also carries a truncheon (1.2.212), as does Duke Francesco della Rovere in the painting, and the Duke's helmet is in the background, with the visor open. Horatio says of the ghost, "He wore his beaver [meaning visor] up" (1.2.243), so Horatio was able to see the ghost's face and recognize it as Hamlet's father.
The most significant parallel among the deaths of Duke Francesco della Rovere, the Player King in the play-within-a-play, and Hamlet's father is that they were all murdered by having a poison liquid poured into one of their ears, which is a unique method of poisoning.
In his letter in the court document, Luigi Gonzago asserts that he accidentally dropped a poisonous lotion into the Duke's ear while he was cleaning it, but he was nevertheless convicted of the murder.
In any event, the performance of the play-within-a-play causes Claudius to implicate himself in the murder of Hamlet's father by suddenly rising and exiting the room, which confirms the ghost's accusations and Hamlet's suspicions.