The Hobbit Chapter 9
The travelers get up the next day and walk in a random direction. After they have gone some distance, wood-elves burst from the trees around them and order them to halt. The elves bind all of the dwarves and lead them off. Bilbo had put on his magic ring and escaped detection. He follows the elves at a safe distance. The dwarves are led blind-folded over a swift river and into the cavern of the Elvenking. Bilbo hustles behind them just as the magical doors close. The tunnels are brighter and better built than the goblin tunnels. In the great hall, the Elvenking orders the dwarves to be unbound so he can question them. The dwarves are impolite and Balin makes the king angrier. The king orders the dwarves to be imprisoned until they tell him what he wants to know. Bilbo wanders around the caverns for many days, barely getting any sleep. After a week, Bilbo finds out that Thorin is being held in a deeper prison cell. He eventually finds that cell. He speaks to a down-hearted Thorin through the keyhole. He tells the other dwarves that Thorin is all right and they all agree to come up with a plan for escape.
Bilbo does not like being so heavily depended on. He thinks long and hard, but nothing occurs to him. Until,
"One day, nosing and wandering about, Bilbo discovered a very interesting thing: the great gates were not the only entrance to the caves. A stream flowed under part of the lowest regions of the palace, and joined the Forest River some way further to the east, beyond the steep slope out of which the main mouth opened." Chapter 9, pg. 175
Elves throw empty wine barrels through a trap door into this stream. They float down the Forest River to the Long Lake where the men of Esgaroth gather them and send them to be refilled. Bilbo observes this and begins to form a plan.
One night, a butler asks the chief of the guards to try some of the wine before it is taken to the king's banquet. Bilbo recognizes this as his opportunity to steal the keys. He follows the elves and watches as they drink too much and fall asleep. Bilbo creeps in and steals the keys. He releases Balin, and they go off after Thorin. Bilbo is upset because the keys are making too much noise, rattling against each other. All the dwarves are reunited but they balk when Bilbo proposes that they get inside empty wine barrels. Eventually, they realize that there is no other option, and they follow Bilbo to the cellar. Balin watches the sleeping elves as the others find the right size barrels. Bilbo gets the thirteen dwarves closed in their barrels, just as one of the elves comes to look for the butler. He wakes the butler and the chief and they all begin to drink some more. After a while, the butler yells for the barrels to be thrown into the river. Bilbo realizes that he hasn't figured out how he is going to go with them. He jumps and grabs the last barrel just as it is thrown into the stream.
In the underground tunnel, Bilbo struggles to stay above water. The barrels float around and start to smash into each other in the current. Bilbo worries about the dwarves and hopes that he sealed all of their barrels well enough. The barrels surface in the river and get stuck around a bend. While they are stuck, Bilbo climbs on top of several of them and manages to stay on top of them once they float back into the current. He is cold and drenched. He floats down river in this fashion and before long, the trees begin to thin. The barrels stop against a shallow stone wall. Bilbo sneaks off to get food, as people on shore lash the barrels together into a raft. In the morning, he wakes and gets on the raft right before some elves start to push it down river again. He stole a bottle of wine and a loaf of bread during the night. They are out of Mirkwood finally, but Bilbo doesn't think that they are out of danger.