Dueling is closely related to the jousting competitions of the middle ages. The development of dueling codes may be related to the Chivalric code of honor practiced by noble knights. A joust is basically a duel on horseback. Formal rules required that jousting competitors be of noble birth. When two knights approached each other at the beginning of a competition, etiquette required that they raise the visors of their helmets, revealing their identities to one and other. This helped assure that only nobles were participating in the fight. This gesture lives on today -- it eventually evolved into the military salute.
The introduction of firearms to European battlefields led to the eventual extinction of heavily armored knights, since no armor was really bullet-proof. Since massive swords were no longer needed to bash through plate armor, this led to the development of lighter swords that could be wielded with more finesse.
As sword fighting shifted to lighter, more skill-oriented weapons, some duelists began to practice the art for sport rather than honor. It became a contest, not to the death, but to a certain number of points. Injuries were still common, even after the Italians started placing a button on the tips of their swords, but fatalities dropped dramatically. The art of swordplay was practiced as the sport of fencing.
As guns became more common weapons in Europe, they affected another major change in dueling -- now, anyone could duel. Buying a pistol was much less expensive than buying a sword. Costly training with an Italian fencing master was not necessary to participate in a pistol duel. This democratized dueling. It was no longer something done only by barons and princes. Dueling filtered down throughout society.
In the United States, doctors, newspaper editors, politicians and lawyers dueled one and other. The breakdown of the "dueling class" was complete -- America didn't have kings or dukes. Duels soared in popularity throughout the United States and Europe until the 20th century.