The word nerd was first used in the 1950 Dr. Seuss book If I Ran the Zoo, in which a nerd was one one of the many oddly named creatures in the titular zoo. According to Ben Zimmer of Vocabulary.com, a 1951 Newsweek article mentioned it as one of the new terms being used by teenagers.
It seems unlikely for teens to have latched on to a single proper noun in a Dr. Seuss book so quickly, but there is no recorded source of the word being used previously.
It’s possible that it was based on the 1940s slang word “nert,” which referred to a stupid or crazy person.
It’s certainly easy to see how teens of the 1950s might co-opt the adults’ term for morons and use it to mean “squares” and people who didn’t understand their culture.
Geek is actually an old English word meaning freak, imported via the German word “geck,” which could also mean fool.
Circuses in 18th century Austro-Hungary used to advertise their “geeks” as their weirdest human attractions, and the word was often used to refer specifically to those whose act consisted specifically of biting the heads off of live animals.
The word had its resurgence when it was used in the popular 1941 book Nightmare Alley and its equally popular movie adaptation, to refer to such.
Most etymologists think that dork is an alteration of the word dick, perhaps coming out of the Midwest, and thus originally meant penis, too.
It was certainly used to mean a penis in the 1961 novel Valhalla, although it was spelled “dorque”; a 1964 article in American Speech confirmed its phallic meaning and spelled the word as “dork.”
It was also used by Charles Schmid, a serial killer known as “The Pied Piper of Tuscon,” who was interviewed in the (then obviously extremely prevalent) Life magazine, in which he was quoted saying “I didn’t have any clothes and I had short hair and looked like a dork.
Girls wouldn’t go out with me.” Schmid almost certainly meant “penis” when he said “dork,” but as the word caught on in pop culture it more commonly came to mean people who look uncool and/or odd.