Cigars had been offered as prizes for early American carnival games. Barkers would declare “close but no cigar” when the player didn’t quite accomplish the task successfully.
Go to a carnival today and you might win a stuffed animal if you win one of the nearly impossible and often-rigged games. It turns out that carnivals in the 19th century were a little different.
In American carnivals of the late 19th century and the early 20th century it seems that adults played to win prizes for themselves rather than for their children. Cigars were a common prize.
A cigar was traditionally one of the rewards at carnivals for winning at games of skill or chance. Coney Island offered many such games in the early 1900s. Most people did not win a prize; for them, the carnival barker would declare: “Close, but no cigar!”
There appears to be some uncertainty about exactly when the idiom originated. Some put the origin in the 19th century and others suggest that it didn’t emerge until as late as the mid 20th century.