Note: I'm only answering from experience here. I'm also answering more from a classical music background.
Part of this is probably psychological - if we already know the melody, it probably makes it easy for us to then pick out the melody even if it has been embellished in some form or another, such as in the Mozart piece you gave as an example.
On the flip side, part of this ability is probably due to performer technique and ability, after the performer has been able to pick out such embellished melodies. For instance, in the piano interlude in Schubert's "Im Fruehling", after "und sie im Himmel sah", the right hand "appeggiations" (for lack of a better term) are probably supposed to mimic the piano melody at the beginning. The actual sheet music in those four measures doesn't really contain anything indicating that the notes comprising the original melody are to be discernible, other than the fact that the A at the very end of the melody is held down (mirroring the original version of the melody).
Rather, the listener and performer are able to distinguish and make discernible the melody in that short passage from 1) noticing the similarities between that passage and the original melody (in both parties) and 2) the pianist thus being able to put slightly more stress on the notes comprising the original melody and make the similarities more prominent.