The word itself appears to derive from the lines "And then, just to show them, I'll sail to Ka-Troo / And Bring Back an It-Kutch, a Preep and a Proo, / A Nerkle, a Nerd, and a Seersucker, too!" in the Dr. Seuss book "If I Ran the Zoo" (1950). (The spellings `nurd' and `gnurd' also used to be current at MIT.) How it developed its mainstream meaning is unclear, but sense 1 seems to have entered mass culture in the early 1970s (there are reports that in the mid-1960s it meant roughly "annoying misfit" without the connotation of intelligence).
An IEEE Spectrum article (4/95, page 16) once derived `nerd' in its variant form `knurd' from the word `drunk' backwards, but this bears all the earmarks of a bogus folk etymology.
Hackers developed sense 2 in self-defense perhaps ten years later, and some actually wear "Nerd Pride" buttons, only half as a joke. At MIT one can find not only buttons but (what else?) pocket protectors bearing the slogan and the MIT seal.