In Shakespeare's time, a geek was sometimes a geck. They were both English dialect words meaning a "fool" or "freak. That came by way of German geck which still exists, also meaning a fool. Dictionaries say that its roots are in the Dutch and Afrikaans adjective gek meaning "crazy." There is also an Alsatian word gickeleshut which is a hat worn by a clown or jester, especially during carnival.
In 18th century in Austria-Hungary, gecken were truly freakish people (as in people with physical abnormalities) who were unfortunately on display in some circuses. By the 19th century, that had moved to North America and a geek was a performer in a geek (or freak) show in a circus, carnival or sideshows at fairs.
That use of the word continued throughout the 20th century, though the term "freak" began to be used in the 1960s to describe "hippies" and counter-culture people and the term took on a more positive spin.
In Jimi Hendrix's song, "If 6 Was 9" he sings:
White collared conservative flashing down the street,
Pointing their plastic finger at me.
They're hoping soon my kind will drop and die,
But I'm gonna wave my freak flag high, high.
Wave on, wave on
Fall mountains, just don't fall on me
David Crosby refers to long hair as a freak flag in his song "Almost Cut My Hair" "I feel like letting my freak flag fly."
Geek evolved in a similar way, especially in the more technology-obsessed 80s and 90s. Author William Gibson was interview in the mid-1980s by a young Neil Gaiman and Gibson said that as a kid he was a "geek who couldn't play baseball." The usage was odd enough that an editor for Time Out magazine changed geek to Greek. Though neither probably made much sense to many readers ("It's all Greek to me."), the word was evolving from a negative connotation to one that could be used with pride.
Geek and nerd are sometimes used interchangeably but there have different connotations. In a 2007 interview on The Colbert Report, Richard Clarke said the difference between nerds and geeks is "geeks get it done."
The similarity may have been that both were usually used to describe males who were socially inept, felt out of place and freakish and yet have some advanced skills in science, math, computers, history, or gaming etc.
The TV show The Big Bang Theory features geeks who are successful and get girlfriends (including the hot non-geeky girl) and the show has become so popular in the culture that there is a kind of "geek chic" movement in fashion and the general youth culture.
Best Buy stores capitalized on the concept by introducing their Geek Squad employees for tech support.
The term nerd is similar when it comes to tech, but perhaps is slight;y less "chic" and associated with thinking of some things as "cool" that the general population might laugh at. The Revenge of the Nerds films signaled a similar rise in positivity towards that term.
So let your geek flag fly. But don't call a geek or a nerd a dweeb. That's a whole other thing.