For the first time the Fermat variational principle, known in geometric optics, is used to analyze the acoustic Doppler effect. On its basis, exact and physically vivid analytical formulas have been derived for the Doppler effect in the general case of sound propagating in a three-dimensional inhomogeneous moving medium.