(I am a woman in my last year of a PhD in low-dimensional topology, I'll be a postdoc in the fall, and I have research aspirations. I'm mostly trying to articulate how I got to this stage in my life. In a real sense, my answer shouldn't matter very much since the many things that keep women away from mathematics clearly didn't work on me too well, or on the several other women that are currently in mathematics; but perhaps it is still valuable to see what worked for me.)
As a woman and an Indian in mathematics, the following is one of my 'favorite' phenomena: stereotype threat. Here is a more detailed non-Wikipedia source. Briefly, there have been studies involving Asian-American girls, where the group who were reminded that they were of Asian descent performed better than the control group on a given math test, and the group that was reminded that they were girls performed worse. It's unclear to me how we should respond to this phenomenon as educators. One possibility is to straight up mention it to students, assuming that awareness might dampen its effects. Another might be to be entirely indifferent to the genders of our students (and play up other similarities. A woman in your class might be the only woman there, but she presumably belongs to shared groups with some of the other students.)
Personally, I grew up in upper middle class urban India, in a part of the country where it simply never occurred to me that women and men could have (markedly) differing mathematical abilities - gender discrimination abounds in India, but at least in our socio-economic class and region, it wasn't pronounced in terms of intellectual ability; in particular, my elder brother suffered from math anxiety - I expect that my desire to compete with him had at least something to do with my interest in the subject. In any case, intellectual pursuits were strongly encouraged for everyone. We revered the academically successful students (class ranks were a matter of avid discussion) and the top positions seemed equally distributed (in my high school, there was an award for highest aggregate score in math and physics in our 12th grade (national) exam, and I recall the genders being equally represented. Nonetheless, there were various such pursuits where I was often the only female participant (inter-school quiz competitions, etc.). The OP mentions such environments as being perceived as hostile to girls and women, but I did not particularly feel this way -- while my fellow participants were not the same gender, we had bunches of other similarities, for example, interest in participating in the relevant activities.
Anyway, as a child I didn't know that one could do mathematics for a living. I like to plan ahead for things (and I believe this is correlated with gender), and my youthful planning for my life didn't include mathematics since I just didn't even consider it an option. My love for mathematics was kindled at a very early age by my grandfather, whose games with my toddler-self consisted of doing additions and subtractions using little circles. Coming to the US as an undergraduate I was excited to learn about the concept of 'double majors' and my decision to pick up a second major in mathematics was motivated in part due to my interest in it, but also due to being a typical over-achiever. My decision to switch to mathematics for graduate study was motivated by the following: math was more intellectually challenging than my primary major (biochemistry), mathematical results contain way more certainty than anything one would be doing in biochemistry, and I just liked my mathematics professors more. As an undergraduate I did attend the Nebraska Conference for Undergraduate Women in Mathematics which was a valuable experience just to know how many women there are in mathematics.
Outside of my own personal experience, I really like the idea of Sonia Kovalesky Days. I was recently involved in an iteration as a panelist, and it was really exciting to see so many young women participating and seemingly having a great time with mathematics. One of the young women asked the panelists to give them names of notable women mathematicians, and I think even knowing about women mathematicians (and women in other STEM fields) would be neat for them. On which note, I'm also a fan of the AWM essay contest.
tl:dr Here are the factors which I personally credit for my being in mathematics:
- Being introduced to mathematics as a fun game as a young child
- Not being exposed to the notion that 'women cannot do mathematics' or that 'men are just better at mathematics than women'
- Ability to find other points of similarity with my fellow mathfans, as opposed to gender
- I am personally not too upset by competitiveness.
- Mathematics is generally difficult, so I consider it a challenge
- Mathematical results are long-lasting. If I've proved something correctly, it lasts forever, unlike most things in the other sciences.
- I like pretty much all the mathematicians I've met (y'all are really nice to young people)
- On most days, I'm not averse to standing out in a crowd, so I'm not too upset about looking quite different from the "average" mathematician
I attribute any success I might have to having excellent advisors, teachers, and mentors throughout my life; they were almost all male, which is unsurprising, since most mathematicians are.
Here are some possible steps one could take as an educator to interest more women in mathematics:
- Do not buy in to the notion that 'women simply cannot do math' (there is some amount of research showing that teachers call on boys more often than girls, etc. Presumably being aware of such findings might help us be more equitable)
- Point out, early on, that mathematics is something one can do for a living, and in fact it has tons of applications in all sorts of places.
- Point out examples of notable female mathematicians
- Encourage participation in/or organize events like the SK Days, the NCUWM, or the AWM Essay contest.