Caves form in limestone (calcium carbonate), and occasionally in dolomite (calcium magnesium carbonate), when water containing dissolved carbon dioxide (carbonic acid) seeps into rock crevices and joints.
The carbon dioxide comes from decaying organic matter in soil, and also directly from the atmosphere. This slightly acidic water dissolves the rock, forming cavities which can enlarge and join up to make larger cave systems of interconnected chambers. An underground water flow can develop when many rain-fed subsurface drainages join up, or a river can be captured by an open cavity collapse structure (sink hole or doline) and flow underground. This leads to further enlarging and sculpturing of the caves by chemical and physical weathering. Gravel, sand, silt and clay can be deposited in the caves from outside, or fine sediments from internal springs may build up. This dissected and dissolved landform with a complex groundwater system is called karst.