Richard and Dom give a good explanation of why not, so I'll just give a proof by demonstrating a specific counter example, which you may find interesting: Telemann's Canonic Sonatas.
These are strictly imitative canons, so the second voice plays a time-delayed repetition of the first. If you consider a melody that contains the phrases A-B-C, then when the first voice begins playing B, the second voice is playing, A (so A and B sound good together). Then, when the second voice reaches B, the first voice has moved on to C (so B and C go well together).
What is astonishing about these sonatas is that they modulate (change keys). This means that phrases A and C might be in 2 different keys, with phrase B cleverly designed to be tonally-ambiguous so that it can fit into either key. Obviously, A and C, being in different keys, can be shown to not work well together. Yet B blends seamlessly with both of them.
Telemann wrote a number of these, but here's one example: