The Battle of the Cowshed occurred fairly soon after the animals gained their freedom from Farmer Jones. It was not unexpected, and the animals were prepared. In fact, Snowball, one of the leading pigs, had already done some research by reading (yes, pigs read in Animal Farm) about the military victories of Caesar in one of Farmer Jones’ books.
Part of Snowball’s strategy was to lure the humans into an ambush by staging a retreat. Early in the battle, the animals made it appear that they were losing, and turned and ran. Once the humans reached the vicinity of the cowshed, the animals, lying in wait, attacked. They were able to push the outmaneuvered humans off the farm.
It’s important to note that later, after Snowball had been chased off the farm by Napoleon’s dogs, Snowball was accused of running away during the battle. This was only true in the sense that he took part in the planned retreat.