At least according to "America's Secret Slang" which I'm watching on TV. Turns out that it was a dance contest that slaves used to participate in for the entertainment of their "owners" (geeze, what a despicable term) and that the prize was actually a cake...hence the phrase "that takes the cake".
Plus I learned many of the language contributions that mine own people...them damn Irish...are responsible. Not the least of which is "nincompoop". Which, you know, is something to be proud of.