IIRC incarceration was a method started by christian monks, and the intention was that the incarcerated would use the time to reflect and contemplate his crimes - a form of rehabilitation.
As you say, that certainly isn't what's happening today. Instead it gives criminals a broader network, and my impression is that it works sort of like a "crime school".
But the US method of privatizing and profiteering off of imprisonment surely must skew the incentives as well.