Poetic justice is a literary device in which ultimately virtue is rewarded and vice punished. In modern literature it is often accompanied by an ironic twist of fate related to the character's own action.
English drama critic Thomas Rymer coined the phrase in The Tragedies of the Last Age Considere'd (1678) to describe how a work should inspire proper moral behaviour in its audience by illustrating the triumph of good over evil. The demand for poetic justice is consistent in Classical authorities and shows up in Horace, Plutarch, and Quintillian, so Rymer's phrasing is a reflection of a commonplace. Philip Sidney, in Defense of Poetry, argued that poetic justice was, in fact, the reason that fiction should be allowed in a civilized nation.
Notably, poetic justice does not merely require that vice be punished and virtue rewarded, but also that logic triumph. If, for example, a character is dominated by greed for most of a romance or drama; he cannot become generous. The action of a play, poem, or fiction must obey the rules of logic as well as morality. During the late 17th century, critics pursuing a neo-classical standard would criticize William Shakespeare in favor of Ben Jonson precisely on the grounds that Shakespeare's characters change during the course of the play. (See Shakespeare's reputation for more on the Shakespeare/Jonson dichotomy.) When Restoration comedy, in particular, flouted poetic justice by rewarding libertines and punishing dull-witted moralists, there was a backlash in favor of drama, in particular, of more strict moral correspondence.