As the title states, why was it necessary to capture the castle before getting further into enemy territory so as to for example capture more vital cities like the capital?
The short and brief answer is because there are people in the castle/fortress, and they're going to be located on major routes you need to support your invasion.
My focus is--admittedly--in the early modern, when the traditional 'castle' had been generally supplanted by the 'star fort' and other fortifications meant to deal with the threats posed by gunpowder based artillery. However, the same general principles of siege--and the importance of siege--apply, and one of the biggest reasons why it's necessary to take castles before moving on is the garrison inside. Were you to leave the fortress untaken, the garrison would not just sit behind the walls and do nothing. They would have free hand to sally out, and do as they will. They can harass your own supply lines and foraging parties. If they're close enough to the border, their garrison can cross into your territory and begin raiding there. The castle can be resupplied, and serve as a base for the enemy's army to manuver, while also increasing your own supply lines. From a less military perspective, it means that the garrison can still go out and enforce control of the surrounding area, effectively denying you that same control.
To add on to this, consider that fortresses and castles were major investments, and would not be built in the middle of nowhere where they could do no good. Fortresses would be constructed in key points where they could dominate the surrounding territory, as well as major lines of communication.
So, leaving a castle untaken leaves your lines of communication lengthened (as they can not use the routes controlled by the castle), vulnerable (to sorties and raids by the garrison of the castle). Furthermore, it provides your enemy bases to either support their own army on campaign, raids against your own lines of communication and foraging parties, or even raids into your home territory. To leave major castles and fortresses untaken while pushing deeper into the enemy's territory would be a great risk.
It is of tactical significance for various reasons. The main ones have to do with having enemy forces inside your captured territory or behind your forces.
If an army were to simply bypass a fortress they would have left behind a hostile garrison which is now perfectly positioned to ambush your army –getting yourself envelope doesn't sound good to me– being now in your rearguard and cutting off your supply line, remember that logistics can be more important than tactics, ask Napoleon why.
Another reason is that castles were not only ment to protect a region but also to stop advances. A well positioned castle is not only the one that is hard to capture, as in the top of a mountain, but also in a valuable position, be it a choking point preventing you from further advance, or protecting valuable resources.
Fortresses in some cases were sometimes also valuable resources by themselves. Capturing one could enable you to have a forward base from which to operate; give you a place to retreat, enable you to stall the enemy's advance or prevent them from using the castle as a production center, as they sometimes were.
There surely are a lot of examples of captains leaving behind an enemy fortress, and some might not have paid the price for it, be it by luck, the opponent's incompetency or the garrison not being a threat.
Tl;dr: Attacks from behind, cutting off supply lines, strategic value or simply not being able to pass by it.
The alternative you suggest was one which was used very successfully by Robert I of Scotland (or The Bruce) in the Wars of Independence against the English during the early fourteenth century. Rather than engage in costly sieges he used highly mobile forces to skirt around the strongholds and inflict punishing raids on the English north. The Scottish in the lacked a significant siege train and also could not afford to be in a fixed location which would allow them to be engaged by the superior English forces.
The practice of avoiding costly sieges went hand in hand with highly mobile warfare. The Scottish rode light ponies, which may have had an Irish origin. They could be ridden throughout the Scottish landscape and were less costly to raise and keep than the heavy warhorses used by most of the English in this period. They and their riders travelled very lightly and needed little to subsist on. Jean le Bel who fought against the Scottish relates:
The fact is that when they invade they’re all mounted, except for the rabble who follow them on foot; their knights and squires ride good sturdy rounceys and the others little hackneys. And because of the mountainous terrain in those parts they have no baggage train and carry no supplies of bread or wine; when they go to war their custom is such – and their abstinence so great – that they make do for long periods with half-raw meat (and no bread), and plain river water (no wine). And they don’t bother with pots or pans: they cook their meat in leather – even in a beast’s own new-flayed hide. They know they’ll find plenty of cattle where they’re going, so they take no provisions, except that each man carries a big flat stone between the seat and panel of his saddle and bags of flour behind him; and when they’ve eaten so much of this ill-cooked meat that their bellies feel tight and bloated, they throw the flat stone on the fire, mix some of the flour with water, and make little pancakes rather like the communion wafers made by the Beguine nuns,1 and eat these to ease their stomachs. So it’s no wonder the Scots cover more ground each day than others do, when, as you’ve heard, they’re all mounted (except for the rabble) and have no carts or baggage
When the Scottish used this highly mobile warfare to raid it was devastating. I made an animated map showing the areas the raids covered. In these raids ideally everything that was not taken, was burnt, it was effectively a chevauchee. During these raids crops were burnt and if it was wet stolen cattle were used to trample them. The Scottish felled trees, destroyed fish weirs, dug up rabbit warrens, and destroyed dovecots. They would time them for the periods that would be most damaging: during harvests or markets and fairs.
When they did take castles or towns it was via subterfuge, in one example a bribe to a guard. Stealth was also used with one town falling due to soldiers being hidden in a wagon. They also used ingenious rope ladders which are describe by the Chronicle of Lanercost. One castle fell after the Scots covered their faces and hid amongst cattle grazing near the walls at night, they placed the ladders and took the castle. The English learnt the lessons the Scottish taught them and took this kind of highly mobile warfare to France during the Hundred Years War in the later fourteenth century.
Sources:
Barbour, John, The Bruce trans. and ed. by A. A. M. Duncan (Edinburgh: Canongate Classics, 1999).
Froissart, Jean, Froissart’s Chronicles, ed. and trans. by John Jolliffe (London: Penguin, 2001).
Maxwell, Sir Herbert, trans. The Chronicle of Lanercost, (Glasgow, 1913).
Slavin, Philip, ‘Warfare and Ecological Destruction in Early Fourteenth-Century British Isles’, Environmental History, 19 ( 2014), pp. 528-550.
Penman, Michael, Robert the Bruce (New Haven: Yale University Press, 2014).
McNamee, Colm, The Wars of the Bruces (Edinburgh: John Donald, 2006).
Usually the castle was the objective, or made holding the objective impossible. I am currently studying the Civil War in the Channel Islands, on Jersey and Guernsey there were castles which controlled the entrances to the harbours on the islands. Whilst the entire island was under Parliament control the Royalists held the castles. As a result Parliament was constantly harassed by canon fire and the main ports on the islands were closed to shipping.
The castles also offered protection to passing Royalist ships and became a staging post for privateers.
Parliament may have controlled the islands, but the actual strategic asset were the castles. Parliaments failure to take the castles on Jersey led to a rebellion (war weariness and some unwelcome religious change) and the island passed back to the Royalists, who held it until 1651 and who's privateers became even more of a menace.
Castles were a huge force multiplier, it meant a small body of men could control a large area.
The Second Manchu invasion of Korea in 1636 was one example where the strategy of bypassing fortresses in order to strike the capital worked. The Manchu Qing army (which by this point also included many Han and Mongol soldiers) took only the major fortifications on the direct route to Seoul, the capital of Korean Joseon dynasty, bypassing the garrisons in the numerous mountain fortresses along the way. They took just seven days to reach Seoul, where the Joseon king had no time to escape to the traditional island refuge of Ganghwa and instead had to go to the nearby Namhan Mountain Fortress. They didn't have time to bring in enough food into the fortress, so had no choice but to surrender after a siege of around 45 days.
This was an extremely risky, even suicidal strategy by the Manchus. The difficulty of maintaining supplies through the mountainous Korean terrain had foiled Chinese and Khitan invaders before, and moreover the Manchus had invaded in the winter. In the end it came down to which side ran out of food first during the siege, but the invasion could very well have ended with the capture of the Manchu emperor by the Koreans rather than the other way round.
However, a recent failed coup had wiped out about 12,000 elite soldiers in the borderlands, and the Korean field commander Kim Jajeom refused to offer resistance to the invaders and dithered in attempts to offer assistance to the besieged Namhan Mountain Fortress. So the Koreans failed to exploit the weakness of the fortress-bypassing strategy of the invaders, and the Manchu gamble worked, just about.