Nietzsche traces the origins of guilt and conscience to the primitive relationship between buyer and seller, creditor and debtor. We are creatures who measure and evaluate everything: everything has a price, deeds just as much as goods. This relationship exists also between people and the community they live in. The community provides shelter, peace, security, and much else besides, placing people in its debt. People who break the laws of their community are not only not repaying the debt, but they are assaulting their creditor. No wonder such offenders face the harshest of punishments.
Nietzsche also observes that the more powerful the community becomes, the less it needs to punish offenders. If the community is weak, any attack against it is life threatening, and such a threat must be eliminated. A community that is strong enough to resist all sorts of assaults has the luxury of letting offenders go unpunished. Such a society has overcome its demand for strict justice. We give the name "mercy" to the expression of power in letting an offender go.
Nietzsche next turns to the origin of justice, suggesting that the reactive affects of revenge and ressentiment are the last to be touched by justice. Very few can truly be just toward someone who has harmed them. Still, the noble man who lashes out against someone who harms him is far closer to justice than the man of ressentiment, who is poisoned by prejudice and self-deception.
Justice and the institution of law essentially take revenge out of the hands of the offended party. If I am robbed, it is justice, and not myself, that has been harmed, and so justice must claim revenge. Thus, Nietzsche suggests, the concept of justice can only exist in a society that has established laws that can be transgressed: there is no such thing as "justice in itself."
We have seen that origins and utility are worlds apart. Anything that has existed for any length of time has been given all sorts of different interpretations, meanings, and purposes by different powers that master and subdue it. That something has a purpose or utility is only a sign that a "will to power" is acting upon it. Things and concepts have no inherent purpose, but are given purpose by the different forces and wills that act upon them.
The concept of punishment, for instance, has an aspect that is enduring and an aspect that is fluid. Contrary to what we might otherwise assume, Nietzsche suggests that the act of punishing is what endures, and the purpose for which we punish is what is fluid. Punishment has such a long history that it's no longer clear exactly why we punish. Nietzsche provides a long list of different "meanings" that punishment has had over the ages.