My theory is as follows: Our bodies (ears) detect the music while we are asleep and send this signal to the brain (as usual), but because a different part of the brain is active while the body is sleeping, the music is processed in the subconscious mind and ends up as part of a dream (if the dream state is active at that stage).