A method for free-energy calculation based on mean-force dynamics (fictitious dynamics on a potential of mean force) is presented. The method utilizes a logarithmic form of free energy to enhance crossing barriers on a free-energy landscape, which results in efficient sampling of "rare" events. Invoking a conserved quantity in mean-force dynamics, free energy can be estimated on-the-fly without postprocessing. This means that an estimate of the free-energy profile can be locally made in contrast to the other methods based on mean-force dynamics such as metadynamics. The method is benchmarked against conventional methods and its high efficiency is demonstrated in the free-energy calculation for a glycine dipeptide molecule.