Butterflies go through a life cycle including stages: egg, larva, pupa, and adult. The metamorphosis from a caterpillar into a butterfly occurs during the pupa stage. In this stage, the caterpillar's old body dies and a new body forms inside a protective shell known as a cocoon.
While some people think of cocoons as a resting place, to the contrary, there's a lot of activity. Inside the cocoon, the caterpillar is transforming into a new creature. This requires that the old caterpillar body be broken down and turned into something new. This is when and where the magic happens!
Inside a cocoon, the same juices it used to digest food as a larva is now used to break down its own body. The fluid breaks down the old caterpillar body into cells called imaginal cells. Imaginal cells are undifferentiated cells, which means they can become any type of cell. Many of these imaginal cells are used to form the new body.
The process of this transformation is known as holometabolism, but I call it MAGIC!