"The phrase, and its variant 'nice try, but no cigar', are of US origin and date from the mid-20th century. Fairground stalls gave out cigars as prizes, and this is the most likely source, although there's no definitive evidence to prove that."
"It is first recorded in print in Sayre and Twist's publishing of the script of the 1935 film version of Annie Oakley:"
"Close, Colonel, but no cigar!"
"It appears in U. S. newspapers widely from around 1949 onwards. For example, a story from The Lima News, Lima, Ohio, November 1949, where The Lima House Cigar and Sporting Goods Store narrowly avoided being burned down in a fire, was titled 'Close But No Cigar'."
http://www.phrases.org.uk/meanings/close-but-no-cigar.html