Mass functions of starless dense cores (CMFs) may arise from contraction and dispersal of core-forming filaments. In an illustrative model, a filament contracts radially by self-gravity, increasing the mass of its cores. During this contraction, FUV photoevaporation and ablation by shocks and winds disperse filament gas and limit core growth. The stopping times of core growth are described by a waiting-time distribution. The initial filament column density profile and the resulting CMF each match recent Herschel observations in detail. Then low-mass cores have short growth ages and arise from the innermost filament gas, while massive cores have long growth ages and draw from more extended filament gas. The model fits the initial density profile and CMF best for mean core density 2 10^4 cm^-3 and filament dispersal time scale 0.5 Myr. Then the typical core mass, radius, mean column density, and contraction speed are respectively 0.8 solar masses, 0.06 pc, 6 10^21 cm^-2, and 0.07 km s^-1, also in accord with observed values.