We derive for the first time the dust mass function (DMF) in a wide redshift range, from z~0.2 up to z~2.5. In order to trace the dust emission, we start from a far-IR (160-um) Herschel selected catalogue in the COSMOS field. We estimate the dust masses by fitting the far-IR data (lam_rest>50um) with a modified black body function and we present a detailed analysis to take into account the incompleteness in dust masses from a far-IR perspective. By parametrizing the observed DMF with a Schechter function in the redshift range 0.1<z<0.25, where we are able to sample faint dust masses, we measure a steep slope (alpha~1.48), as found by the majority of works in the Local Universe. We detect a strong dust mass evolution, with M_d^star at z~2.5 almost one dex larger than in the local Universe, combined with a decrease in their number density. Integrating our DMFs we estimate the dust mass density (DMD), finding a broad peak at z~1, with a decrease by a factor of ~3 towards z~0 and z~2.5. In general, the trend found for the DMD mostly agrees with the derivation of Driver et al. (2018), another DMD determination based also on far-IR detections, and with other measures based on indirect tracers.