As I understand it, Hulk, Thor, Spider-Man, the X-Men, the Fantastic Four, Iron Man, and many others were created in their own narrative universes (stand-alone comics), and the single shared universe we see in the MCU movies only emerged ad-hoc out of various Marvel comics cross-overs published over a substantial span of time. Or were some Marvel characters part of a shared universe from their inception? In other words, did Stan Lee et al populate what was intended from the outset to be a single universe with all these diverse characters, or was it a creative after-thought?
As early as 1940, some of Marvel's comics shared a Marvel Universe. And, much like with the MCU, the Avengers was where it all really came together in the 1960s.
Some of Timely Comics (the 1930s and 40's predecessor to Marvel Comics) characters coexisted in the same world was first established in Marvel Mystery Comics #7 (1940) where Namor was mentioned in Human Torch's story, and vice versa. Later several superheroes (who starred in separate stories in the series up to that point) met each other in a group dubbed the All-Winners Squad.
Though the concept of a shared universe was not new or unique to comic books in 1961, writer/editor Stan Lee, together with several artists including Jack Kirby and Steve Ditko, created a series of titles where events in one book would have repercussions in another title and serialized stories would show characters' growth and change. Headline characters in one title would make cameos or guest appearances in other books. Fantastic Four #12 is the first crossover comic book in modern Marvel continuity (first meeting of Fantastic Four and the Hulk). Eventually, many of the leading heroes (Ant-Man, Wasp, Iron Man, Thor and the Hulk) assembled into a team known as the Avengers, which debuted in September 1963. This was not the first time that Marvel's characters had interacted with one another—Namor the Sub-Mariner and the original Human Torch had been rivals when Marvel was Timely Comics (Marvel Vault), under editor Martin Goodman — but it was the first time that the comic book publisher's characters seemed to share a world. The Marvel Universe was also notable for setting its central titles in New York City; by contrast, many DC heroes live in fictional cities. Care was taken to portray the city and the world as realistically as possible, with the presence of superhumans affecting the common citizens in various ways.