We report on time-resolved linear and nonlinear terahertz spectroscopy of the two-band superconductor MgB$_2$ with the superconducting transition temperature $T_c approx$ 36 K. Third-harmonic generation (THG) is observed below $T_c$ by driving the system with intense narrowband THz pulses. For the pump-pulse frequencies $f=$ 0.3, 0.4, and 0.5 THz, temperature-dependent evolution of the THG signals exhibits a resonance maximum at the temperature where $2f=2Delta_pi(T)$, for the dirty-limit superconducting gap $2Delta_pi=$ 1.03 THz at 4 K. In contrast, for $f=$ 0.6 and 0.7 THz with $2f>2Delta_pi$, the THG intensity increases monotonically with decreasing temperature. Moreover, for $2f<2Delta_pi$ the THG is found nearly isotropic with respect to the pump-pulse polarization. These results suggest the predominant contribution of the driven Higgs amplitude mode of the dirty-limit gap, pointing to the importance of scattering for observation of the Higgs mode in superconductors.