The legal system does this for two reasons.
First, young people are not believed to have as much control over themselves or as much understanding of what they are doing as adults do. For this reason, they are not believed to be as culpable for their actions as adults are.
Second, young people are believed to be less set in their ways. They may have committed crimes, but they are not seen as hardened criminals. Instead, there is a belief that they are still susceptible to being rehabilitated.
For these reasons, young people are treated differently. They are not held as accountable for their crimes as adults. When they are punished, there is more of an emphasis on rehabilitation so that they can break their tendency to engage in criminal actions.