Like you others have said, its about balance and placing the humor at the right moment. Don't have Thor's mom die and then 5 seconds later cut to wacky pantless antics from Stellan Skaarsgard, there's no need for Beyonce jokes when Doctor Strange is trying to learn magic, and there was no reason to have every other sentence out of Ultron's mouth be a quip. Balance.
Also, let's remember, Marvel's best, most iconic stories from the comics have always been its most serious and intense ones, the ones that put the hero through the ringer and have them overcome adversity and their own demons: Kraven's Last Hunt, Death of Gwen Stacy, Demon in a Bottle, Death of Captain America, pretty much all of Frank Miller and Brian Bendis' Daredevil comics, The Dark Phoenix Saga, the original Civil War, Annihilation, Ironman: Extremis, X:Men God Loves, Man Kills etc. etc. etc.