Mercury has a global magnetic field of internal origin and it is thought that a dynamo operating in the fluid part of Mercury's large iron core is the most probable cause. However, the low intensity of Mercury's magnetic field-about 1% the strength of the Earth's field-cannot be reconciled with an Earth-like dynamo. With the common assumption that Coriolis and Lorentz forces balance in planetary dynamos, a field thirty times stronger is expected. Here I present a numerical model of a dynamo driven by thermo-compositional convection associated with inner core solidification. The thermal gradient at the core-mantle boundary is subadiabatic, and hence the outer region of the liquid core is stably stratified with the dynamo operating only at depth, where a strong field is generated. Because of the planet's slow rotation the resulting magnetic field is dominated by small-scale components that fluctuate rapidly with time. The dynamo field diffuses through the stable conducting region, where rapidly varying parts are strongly attenuated by the skin effect, while the slowly varying dipole and quadrupole components pass to some degree. The model explains the observed structure and strength of Mercury's surface magnetic field and makes predictions that are testable with space missions both presently flying and planned.