Your entire guitar vibrates when you play it: the top most of all, but even the neck and headstock are moved by the vibrating strings’ energy. Any loose part on your guitar may buzz or rattle audibly, sometimes only when certain notes are played. Parts don’t have to be visibly loose to rattle, and it can be quite a job to chase down the causes of some of these little noises.
If the dirt buildup is really bad, wiping with a damp cloth won’t suffice. Use some extra-fine wool to scrub gently across the fretboard, parallel to the frets.