The primary purpose of incarceration is not rehabilitation or retribution, but rather crime prevention. Convicts aren't out committing more crimes (at least not among the general public).
That's incarceration. My comment is about justice in general. Incarceration is only one of many avenues of justice. Others include capital punishment, corporal punishment, fines, asset forfeiture, exile, expulsion from school, or registration on a public list (such as the sex-offender registry).
The effects of all of these vary widely, and many of them have very long-running debates on their merits (or lack thereof). I think it's premature to claim that any one of these concepts has a primary purpose, let alone a universally agreed-upon one.