It doesn't matter (a lot) how this happens initially.
Once the person stops participating in the mainstream popularity contest they are necessarily going to be left behind and it is going to affect their persona.
I know both types of "nerds". Ones that became nerds after not being popular and ones that were never interested.
I, for example, started my journey to being nerd before the contest. I started reading books when I was 6 and by the time I was 7 or 8 I would be spending large most of my time either reading books or searching through books in the library. I would be coming back home with bags full of books once a week. I never went to preschool and I never had any peers (other than my siblings) to interact with before I started spending most of my time reading. By the time I went to school I was not interested to compete with other kids for anything and I was quite ok being left to my own interests.
I also know people who turned as a mechanism to cope with their apparent loss in the contest. But in my mind that's not strictly "nerd". Nerd is somebody who does what they do because they want to do it and not for some other reason like popularity in their field or as an advantage at work. People who turn to other things as a mechanism to cope typically are less passionate about what they are doing, at least that's my observation.