Why are people surprised about this? Tolkien seems to have loved Deus Ex Machina. A theme of the Hobbit is: Dwarves get caught, Bilbo saves them. Sometimes Bilbo gets caught as well, in which case Gandalf saves them. It's a running theme of the whole movies/book. Just like what I mentioned about Lord of the Rings. A giant force appears at just the right convenient time to save the day.