But in this case wouldn't it be more realistic (and safe) to perform an orderly retreat?
Sometimes they do fall back, most times they'll run away. It probably depends on the quality of the troops involved. Doing an orderly disengagement from combat is probably the most difficult thing a commander ever had to do on a battlefield. Once troops had advanced into combat, they were effectively out of control of the commanders, and ordering them to do anything was difficult.
For example, everyone knew that a bayonet charge was the most effective way to dislodge an enemy. Everyone wanted to do it. But the troops would stop and start firing on their own instead of advancing to bayonet distance. Sometimes they wouldn't advance until their ammunition was gone. And all that fire seldom caused many casualties, as the troops invariably fired too high.
A square is difficult to dislodge. The troops won't run away from the safety of the square, because they're safer in the square than out of it, even running away. The square will not disband because they'll instantly be ridden down by cavalry. So the square isn't operating under the same constraints as a unit in line. They'll stand much longer. There is even one recorded case of a unit forming square to defend against infantry, although this was considered highly unusual. At least the troops couldn't run away.
A unit in line approaches a square and begins firing, and takes return fire. The square does not move. Eventually the line has enough and moves away when they're unable to make the square retreat. Higher quality troops will stay and fight longer. Eventually the square takes enough casualties that it leaves the field. But from what I've seen, the square will not lose it's formation unless it gets a rout result for whatever reason, and that takes a while.
A bayonet charge against a square is difficult, because in almost all cases, one unit will rout rather than fight. The square generally won't rout, so it will be the attackers who retreat.
Units don't take fatigue from marching nearly as much as they do from being in combat. These guys could march all day, and fight at the end of it. About the only time you'll see a unit accumulate fatigue from marching is when they're going uphill. But a short period of combat will produce considerable fatigue.
Hook