The Oxford English Dictionary says that it comes from a shortening of the word doodle 'a silly or foolish fellow', rarely attested throughout the 17th, 18th, and 19th centuries (as in Yankee Doodle Dandy). The first citation for dude dates to 1877, where it's precise meaning is unclear, but it's compatible with an otherwise well-documented sense of "fop, dandy" that has fallen out of use since the middle of the 20th century.
The second-earliest attested sense, 'A man from the eastern United States; a male city dweller, esp. one unused to ranching or to rural life in general', dates to 1884.
The first citation compatible with the modern, very general sense 'a man' dates to 1895, from children's books by a Democratic congressman from New Jersey.
The word's use as an exclamation is only attested (at least according to the OED) from 1989, but it's important to note that such a usage was almost certainly current at the time of writing and predates the written record--and this goes for any word in any sense.
Hey. While technically related to history, your question might be more appropriate for /r/etymology.
Good luck!