It's also because prison and what we call law might not be the most "justice" there is. It's mostly revenge.
Mostly in places with "old testament" attitudes towards morality. For contrast consider cells and prison quality in a place like Norway or Sweden for example (including prison time), compared to the US or some place in Latin America...
There have been many philosophical justifications by judicial philosophers and political experts on justice systems in various countries, and deterrence and rehabilitation are more often than not the stated goals.