The reasoning behind all this is all very contract-based and mundane. According to Variety, the insertion of this scene in Spider-Man 2 happened because the film's director Marc Webb "had an existing contract with Fox Searchlight to helm another film following 2009′s 500 (Days) of Summer. After The Amazing Spider-Man in 2012, Webb’s negotiations briefly stalled with Sony as he was caught in a tug-of-war with Fox. Fox agreed to allow Webb to direct Sony’s Spider-Man sequel, but only if Sony would promote its X-Men film for free." Hence the mid-credits stinger.