There are at least two reasons. The first is that a castle is usually located on the most strategic ground in the area, a hill, river, etc. Basically, it is, or controls, the most valuable "real estate' in the region. If an attacking army controls the "rest of the region" without controlling the castle, it probably hasn't achieved much.
The second reason is that the castle contains the enemy army. Capturing the castle means capturing the opposing army, and thus winning the war. If an attacker leaves the opposing army at large while it goes about its business, it becomes a "free for all," anything can happen, not all of them good for the attacker.
There are times when bypassing the castle is a good strategy. That is when the castle has no inherent value and the surrounding territory is much more valuable. But that's the exception, not the rule.
One exception to the rule was a "castle" called the Alamo. This was defended by less than 200 die-hard Texans determined to "never surrender or retreat, victory or death." It had more symbolic* than strategic value.
Santa Anna should have left a small part of his army to "screen" the Alamo and continued his main plan to chase the Texas government and the main Texas army under Sam Houston. The actual result was the loss of 500 men trying to storm the castle (a multiple of the defenders), and two precious weeks. This gave Houston enough time to regroup against Santa Anna's weakened and demoralized army.
*One reason why Santa Anna wanted the Alamo was because his brother in law had been humiliated earlier when some 300 Texans had infiltrated the fortress, and threatened to shoot down 1500 Mexican defenders in their own fortifications. The brother-in-lawbrother-in-law had surrendered the castle in exchange for "the honors of war," the right for his army to leave unmolested, without being taken prisoner.