When I studied composition with the late Leonard Stein, one of my regular assignments was to compose minuets for string quartet "in the style of Mozart and Haydn." It was no accident that Stein employed the singular form of the word "style." The two composers share a common musical idiom; there are many reasons why inexperienced listeners find it hard to tell them apart.
Haydn and Mozart were both Austrians who spent much of their professional lives in Vienna. A generation separated them, but Mozart's 35 years were entirely contained within Haydn's 77. And there were family connections: Michael Haydn, Joseph's younger brother, also a musician, lived and worked in Salzburg, where Mozart was born and grew up. He was a friend of the Mozart family. (Mozart's duos for two violins were originally ghostwritten as a favor for Michael, struggling with alcoholism and unable to complete a commission.)
The two great composers were certainly aware of each other for many years before they met. In addition to his younger brother's firsthand reports, Haydn would have read published accounts of Mozart's exploits as a child prodigy. And by the time Mozart came to maturity, Haydn was already the most celebrated composer in Europe; knowledge of his influential scores was de rigueur for any serious contemporary musician.
Later, they were members of the same Masonic lodge in Vienna, and became personal friends as well as mutual admirers. This last is noteworthy, especially with respect to Mozart, who was often scathing about colleagues. When he spoke of Haydn, however, it was with reverence. His six great string quartets were dedicated as a set to the older composer, partly as acknowledgment of how much he had learned from Haydn's own essays in the form. Haydn's later quartets are said to have been influenced in turn by the quartets Mozart wrote under his influence. After Mozart's death, the older composer even seems to have experienced something akin to survivor's guilt; he declined a request to write string quintets and refused permission for his early operas to be performed, on the grounds that Mozart's work in these genres was supreme.
So, considering their closeness in time and space, their friendship, and their acknowledged mutual influence, it's not surprising that their music, to the casual ear, sounds similar. Nevertheless, on close listening, their individual voices, their personalities and temperaments, emerge as very different. After only a few measures, an experienced listener usually knows which of the two is being performed.
One difference derives from what might be considered social class. Haydn was a countryman, son of a wheelwright, his family still part of the peasantry (for this very reason—or rather, for its democratic implications—Beethoven treasured a painting of the little hut in which Haydn was born); whereas Mozart was a townsman, his father was university-educated (trained as a lawyer, in fact, although he chose music as a profession) and the author of a best-selling book (on the art of violin playing). Salzburg was a provincial town, but the Mozarts were sophisticated and well-traveled provincials.
The era privileged elegance and wit in its music above almost all other qualities, and Haydn, despite his rustic origins, was hardly lacking in either. But there are various ways of being elegant and witty. Oscar Wilde and Mark Twain both incontestably possessed elegant and witty minds, but no one would mistake either one for his near-contemporary. With that sort of distinction in mind, let's consider two symphonic minuet movements, one by Haydn, one by Mozart. First, from Haydn's 92nd symphony. And from Mozart's 36th symphony. The minuet is a courtly, aristocratic dance, and both composers are faithful to its nature. But Haydn's example is nevertheless rougher, heartier, earthier, like one of those Jane Austen men who are at ease in a London sitting room but back in Hampshire have mud on their boots. Mozart usually keeps his surfaces smoother than Haydn, achieving a more polished suavity.
You can hear, I believe, a comparable difference in the following two string quartet openings: Haydn is sturdy, jaunty, and clearly delighted to begin a string quartet with such a rattling good tune. Mozart's work has a decidedly finer grain, possessing supernal grace along with a certain emotional reticence. This quartet, incidentally, was a favorite of Beethoven, who as a young man copied out the entire piece by hand in order to master its intricacies.