1. [mainstream slang] Pejorative applied to anyone with an above-average IQ and few gifts at small talk and ordinary social rituals.
2. [jargon] Term of praise applied (in conscious ironic reference to sense 1) to someone who knows what's really important and interesting and doesn't care to be distracted by trivial chatter and silly status games. Compare geek.
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, where ‘nurd’ is reported from as far back as 1957; however, knurd appears to have a separate etymology.) 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.
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.