Note: Other answers have addressed various aspects of the posted question quite well. This answer focuses on a request made in a comment beneath the currently accepted answer: "Can you trace the beginning of the negative connotation of the phrase though?" The answer to that question, it seems to me, is relevant to the poster's more general questions about how the saying came to be, because it sheds light on early sarcastic/ironic/exasperated use the expression.
The assumption that "take[s] the biscuit," although frequently used in a negative or sarcastic sense in the UK, was never used similarly in the United States is not supported by the evidence of early U.S. usage of the expression. Following are five instances from U.S. newspapers of the 1880s in which "takes the biscuit" appears in a negative, sarcastic sense.
From a brief item in the [Honolulu, Hawaii] Pacific Commercial Advertiser (June 19, 1880), reprinted from the Chicago [Illinois] Tribune:
Joe Cook calls Niagara "a dateless roar." When it comes to chopping up the English language beyond recognition, Joe takes the biscuit. —Chicago Tribune.
From "City Globules," in the St Paul [Minnesota] Daily Globe (May 30, 1881):
Yesterday the concern published in this city, but devoted to the interest of Minneapolis, bloviated about a granger who was tackled by a confidence gang and unwound of all his ducats. With characteristic stupidity the blacklegs are mentioned as having held forth at the Metropolitan hotel in this city. For superior penetration of the hind sight order this confession takes the biscuit. The joke that a half dozen "con" men could go from here to Minneapolis, when known by the fly soppers of that burg, and take a trick under their very noses is too good to keep.
From a brief item in the [St. Clairsville, Ohio] Belmont Chronicle (February 7, 1884):
For real good old-fashioned muddy mud this town takes the biscuit. If this thaw continues the streets will be navigable for skiffs. Bellaire Independent, Friday.
From a series of related brief items in the [Tombstone, Arizona] Daily Tombstone (July 12, 1886):
The gall of Banker Henderson of Tucson takes the biscuit.
Consistency thou art a jewel! How is that Henderson.
Considerable merriment was caused in the Board of Supervisors this morning by the reading of D. Henderson's communication.
The DAILY TOMBSTONE would inform Mr. D. Henderson of Tucson that even if he thinks the Board of Supervisors have no brains, that they have sense enough to see that he and his associates have not brains enough to rob the people of Cochise county.
And from an untitled item in the [St. Clairsville, Ohio] Belmont Chronicle (September 6, 1888):
For unadulterated "cheek" the St. Clairsville Gazette takes the "biscuit." When the consciousness that the clamor of its party by its President and press in favor of free wool unsettled the market, and caused a depreciation of prices, it has the hardihood to charge the low price of wool to the Republican press. If there is a Democrat in Belmont county who does not know that the "Mills bill" and a Democratic President's declaration in favor of removing the tariff on wool, was the direct cause of the low price of wools at the opening of the season this year, he should be sent to Barnum as a curiosity.
In each case "takes the biscuit" is being used in the same ironic sense in which many U.S. English speakers today use the phrase "takes the prize." It's not that they think biscuits or prizes are inherently or even usually bad; it's that they are making a sarcastic logical inversion in which a presumptively good thing (a biscuit or a prize) is awarded for a bad action, performance, production, or state of affairs.