This comedic effect is most often produced by juxtaposition. For example, an extraordinarily sad piece of music could be used over a montage of mundane events that are not themselves very sad. Because of this, the comedic value of the montage is enhanced by the sad piece of music, or in some cases, completely creates the comedic value of the content. Just the same way, extraordinarily happy music can be employed, heightening the tension between what is heard and what is seen.
The emotional quality of the music is not the only thing that’s important. Sometimes genres can be used to juxtapose with visual content. For example, hardcore gangster rap could be used over a scene featuring ordinary business people, as is done to great comedic effect in the film "Office Space." Just the same, overly saccharine music could be used over a scene that depicts grotesque and explicit violence, as is often the case in films by acclaimed director Quentin Tarantino.
Of course, the music can be comedic on its own merits. If this is the case, the comedic quality of the music will be created by its lyrical content. One of the best examples of this is Weird Al Yankovic, who is world famous for his parodies of various popular songs. However, not all comedic music is necessarily parody. For example, one can think of the many popular and amusing songs that were composed and performed by Andy Samberg when he was a part of Saturday Night Live’s cast.
Ultimately, music for comedy can be of any form. Most often, ironic juxtaposition is the factor that makes a particular piece of music comedic in its circumstance. However, as has already been noted, music can be comedic on its own merits.