It's thought to have come from the slang term "dude" used by the "dude ranchers" of the American West. These were wealthy young men who came to the West for sport and adventure, and their extravagant fashion and mannerisms were considered outlandish and humorous.
The term "dude" eventually spread across the country, becoming a popular slang term for a young man, particularly one who was fashionable or stylish.