There's a big section on this in my psychology textbook. It's way too long to type out here, so I'll try to paraphrase a summary of it as best as I can.
Research has shown that young children develop a gender identity very early, and conform to these identities very strictly. In fact, toddlers are often called the "gender police" because they seem to have a very vivid concept of what is male (i.e. trucks, muscles, etc) and what is female (ie. dolls, soft cute things, etc) and get all worked up when they see something that defies these rules.
However, at about the age of 4 or 5, when kids enter school and are regularly exposed to other kids, these gender lines start to blur a bit and it becomes more complex. Due to the way our society works, girls gain status by acting more like boys (independent, assertive, playing in mud, etc) while boys loose status by acting like girls (being big sissies and playing house, for instance).
Of course, I probably butchered that explanation, but it makes sense if you read it.