In Thor (2011), Lee appears among many people at the site where Thor's
hammer Mjolnir lands on earth. He tears the back off his pickup truck
in an attempt to pull Mjolnir out of the ground with a chain and
causes everyone to laugh by asking, "Did it work?".

In Captain America: The First Avenger (2011), Lee portrayed a general
in World War II, who mistakes another man for Captain America/Steve
Rogers, commenting, "I thought he'd be taller."

In The Avengers (2012), Lee makes a cameo appearance as a random
citizen in the park asked about the Avengers saving Manhattan. Lee's
character responds, "Superheroes in New York? Give me a break", and
then returns to his game of chess. He also appears in a deleted scene,
apparently as the same character: when a waitress flirts with Steve
Rogers, he says to him, "Ask for her number, you moron!"
(source:Wikipedia)

So these random appeararances conflict with the shared universe, especially for the case of Captain America: The First Avenger.

So how does Stan Lee's random appearances work in the Marvel Cinematic Universe?

1 Answer
1

Stan's appearances are a sort of wink to the screen, an in-joke to those that know who he is. All of the characters that he's appeared as have had no material signifigance to the universe, they're just there for on-screen fluff. There's no need to try and retcon these characters, they're all different characters played by Stan with no real story purpose.

To elaborate more on DForck42's answer, it is an easter egg type of feature and a trademark. Much like Hitchcock appearing in all of his movies with eager fans trying to find him. The difference being fans are always eager to see what crazy role he'll be playing. It's an ode to the man who created (or had a role in creating) all of these characters.
–
user4308Mar 7 '13 at 15:49