Fossil mammal sites of late Miocene age (ca 9 Ma) occur in hourglass-shaped, non-interconnected cavities up to 15 m deep, hosted in mudstone (mostly sepiolite), chert and carbonate bedrock in Cerro de los Batallones. This paper provides a model for the sedimentary infilling of the cavities, which functioned as traps for vertebrate faunas and contain one of the richest and best preserved Neogene mammal assemblages of the Iberian Peninsula. Generation of the mammal-bearing cavities started with the solution of underlying evaporites, which resulted in fissures that were subsequently enlarged by subsurface piping, a process rarely preserved in the ancient sedimentary record. The system of subterranean cavities evolved into a pseudokarst landscape, resulting in doline-like shafts reaching the ancient land surface. The sedimentary infilling of the cavities comprises both clastic and carbonate lithofacies that were investigated by outcrop observation, standard and scanning electron microscope petrography, mineralogical analysis, and stable isotope geochemistry. Gravel and breccia talus deposits, clast and mud-supported gravel, pebbly sandstone and mudstone are common detrital infill deposits mostly derived by overflow erosion of bedrock. The deposits containing the mammal bones are marls, and occur both in subsurface cavities and doline-like depressions. In the underground cavities, marlstone was mainly of clastic origin and accumulated in ponds scattered over the floor of the cavity. In contrast, marlstone deposits in the surface dolines formed mostly as a result of biochemical carbonate deposition in small shallow lakes subjected to fluctuation of the water level. The δ18O and δ13C carbonate values indicate different origins for the two kinds of marls. During the final phases of pipe infill the doline marlstone sealed the mammal sites, usually off-lapping the adjacent bedrock.