First of all, let's focus on harmony (notes played at the same time) as opposed to melody (notes played one after another). What sounds good in a melody is quite culture-dependent, but there are reasons why harmony is more universal.
Second, let's focus on sounds that are produced by something long and narrow. In a guitar, violin, or piano it's a string, and in a flute it's a column of air. The physics of vibrations goes so that in such a case the sound is composed of harmonics: sine waves of frequencies f, 2f, 3f, 4f, ... If the shape is different (say, a circular membrane of a drum), then this may not apply.
Suppose we add a second sound, whose fundamental frequency is, say, 3/2 f. This means that its harmonics are 1.5f, 3f, 4.5f, 6f, 7.5f, 9f, ... Half of these (3f, 6f, ...) coincide with the harmonics of the first sound, so the sounds "reinforce" each other. More generally, if the ratio of the frequencies is p/q for some integers p and q, then there will be overlap in the harmonics. And the smaller p and q are, the more overlap there will be.