
That smell of wet earth when the rain begins has a name: petrichor. And the rain doesn't make it, some residents of the soil do. Certain bacteria produce a molecule called geosmin, which builds up in the dry earth waiting for its moment.
The moment comes with the first drops. As each one hits dry, porous soil, it traps air bubbles that burst and shoot out like a fine spray, loaded with geosmin. That's why the smell appears all at once just as the rain starts: you're breathing it in as an aerosol.
The astonishing part is your nose. Humans detect geosmin at concentrations of a few parts per trillion; a nearly non-existent trace is enough for us to recognise that smell instantly. It may be a legacy from when smelling water and fertile soil was a matter of survival. You're smelling ancient bacteria, and it seems to you like the smell of clean.