Ribozymes, which carry out phosphoryl-transfer reactions, often require Mg(2+) ions for catalytic activity. The correct folding of the active site and ribozyme tertiary structure is also regulated by metal ions in a manner that is not fully understood. Here we employ coarse-grained molecular simulations to show that individual structural elements of the group I ribozyme from the bacterium Azoarcus form spontaneously in the unfolded ribozyme even at very low Mg(2+) concentrations, and are transiently stabilized by the coordination of Mg(2+) ions to specific nucleotides. However, competition for scarce Mg(2+) and topological constraints that arise from chain connectivity prevent the complete folding of the ribozyme. A much higher Mg(2+) concentration is required for complete folding of the ribozyme and stabilization of the active site. When Mg(2+) is replaced by Ca(2+) the ribozyme folds, but the active site remains unstable. Our results suggest that group I ribozymes utilize the same interactions with specific metal ligands for both structural stability and chemical activity.