When hot magma (shown in red) rises through the earth, it often does not reach the surface but rather forms large underground magma bodies.
Around the edges of the cooling magma, hot mineral-rich solutions are given off (blue drops moving up). These mix with cooler circulating groundwater, which comes from rainfall (blue drops moving down). The mixture circulates throughout fractures and cracks that form around the cooling magma body.
Overtime, the solutions rich in elements cools and minerals crystallize in any existing openings in the surrounding rock. Minerals such as beryl and tourmaline need unusual elements like beryllium or boron which are derived from cooling magma.