The short answer is: it depends on many variables. But YES, this is possible.
The long answer requires at least some variables be addressed.
- What magic, if any, is in play? Are these all humans, or are other races involved? Assumptions: no magic, only humans.
- What technology level are we at? Advances in farming make it easier to support larger populations with less land. Likewise, advances in siegecraft make it easier to break a wall, vs advances in construction to make a stronger wall, and to make it easier to defend a wall with fewer soldiers. Also relevant would be medical technology for the castle (to prevent plague, either natural or from the besieging army). Assumptions: high middle ages, European technology. Say, 1250AD.
- How good is the area for farming? How "rich" is the soil, is there enough moisture, how long is the growing season, etc. Assumptions: the defenders are using multiple crops to prevent soil depletion, and are in England-like conditions. They average 10 bushels of grain per acre (a real amount for the time and location). We will assume no droughts or other uncontrollable factors occur during the siege. We are vastly simplifying this section, just go with it.
- How long are we dragging the siege out? We will say the fortress needs to sustain itself for 25 years. This is an absurd siege length, but we're going with it anyway. We will further assume they have stockpiles of leather, metal, wood, reeds for gambeson, etc to last that time. Firewood is being deliberately left out of our calculations for simplicity.
- What is the morale of the defenders? Even if the castle can hold out indefinitely, the individuals within may not be willing to. Assumptions: the defenders are highly motivated to never give in. The besieging army takes no quarter and wants to wipe out all the heretics in the defending castle, and all the defenders know this.
For a location, we'll say a plateau peninsula with a narrow crossing attaching it to the mainland, ocean on three sides, and high steep cliffs from ocean level to the plateau. This means there is no landing for boats. We'll say the land entrance is sloped upward to give advantage for the defenders, and the ground in the pass is very rocky, making it hard to tunnel underneath. The wall at the pass is 4 meters thick (12 feet) and 12 meters tall (39 feet), with arrow slits, rounded towers, and all the best innovations for this time period. Conceivably, such a location and fortification would be nigh impossible to take directly (remember: no cannons for this time period).
We will further say the attackers are unwilling or unable to use biological attacks, or the defenders for some reason are capable of neutralizing them. In other words, no rotting cows hurled into the fortress to spread disease.
Most historical "castles" were small affairs. We're assuming this is a proper military fort, not the fortified manor of a knight or minor lord. Let's set the garrison at 800 fighting men (including men at arms and knights), which is quite large for the time period but within the realm of possibility (2000 men is probably the largest garrison of any castle in the period).
Roughly 300 peasants, craftsmen, clergy, and other supporters were required to support one knight's family in England during this time period. A fort with 800 men would likely have 50-70 knights. Let's say 750 soldiers and 50 knights. That's 15,000 people just for the knights, plus the garrison, for 15,800 so far. Then we need additional people to support the 750 men at arms.
That 300 number is derived from averages in England during the time period for knights living in separate manors. We can assume we'll need fewer craftsmen, for example, since all the knights are in one location instead of spread out. I can't find good numbers for this, so we are going to say that 15,200 people can support the entire 800 man garrison. That gives us a total population of 16,000 people behind the walls for the 25 year siege.
For reference, 16,000 people is nearly the size of London in 1200AD. If we say our numbers (the 300 per knight) were bad, and halve the total to 8,000 people (7,200 support and 800 garrison), that is still a large city by the standards of 1250AD Europe.
Now, it takes an average of 20 bushels of grain to support a person for a year. With our average of 10 bushels per acre, that means 2 acres per person. With 8,000 people, that's 16,000 acres being actively farmed. With crop rotation, some of the farmland is not being farmed at any given time. If 2/3 are being farmed and 1/3 is left fallow, we're at 24,000 acres of land necessary to support the people, plus whatever land they need to actually live on, have shops, barracks, etc. We are ignoring that land for simplicity.
That does not include the resources for maintaining horses for the soldiers. Other livestock could graze on the unplanted land, but war horses can't be left out to roam around, since they need to be available for soldiers. And make no mistake, they will be needed for patrols: our assumed location may be impossible to reach by boat, but the defenders will have to patrol their cliffs for any attackers trying to scale them. There are many historical examples of castles falling because small groups made it in past "impossible" mountains or cliffs.
24,000 acres is about 97 square kilometers, or 37.5 square miles. Double that number if you want the original 16,000 people, to 194 square kilometers or 75 square miles. For reference, 97 square kilometers is about the size of Disney World. 194 square kilometers works out to be about 4/5ths the size of modern Edinburgh, Scotland.
Can you enclose that much land, with that many people, behind a wall and maintain it for 25 years? Absolutely, given the physical features described above. But it is not normal for any civilization or time period I know of. It would be nigh impossible to find that perfect location with enough arable land to pull it off. If you reduce the numbers involved, perhaps making this a fort with only 100 defenders and their supporters, you can drop the size considerably, making it easier to find such a location. If you conscript the farmers to hold the wall during active fighting, you could maintain a longer wall.
Furthermore, rationing would reduce the food needed from 20 bushels per person per year to 15 bushels. Anything less than that is going to cause problems if maintained over such a long time period. If the fort starts the siege with existing food storage, they could be producing less food than they need, but coast through with the storage; this puts a timer on how long they last, but a sufficient amount of storage with close enough food production means they could last 25 years.
Sidenote: This assumes no additional refugees fled to the fortress and need to be fed while being unable to productively do anything. Or that some of the land behind the walls was unused, and all the refugees are able to become productive on the unused land. Or that enough casualties are taken early on that refugees take over farming areas previously farmed by now-dead people.
Sidenote 2: See Attack on Titan for a fictional, self-sustaining mega fortress. Some, though certainly not all, of that show could get your mind thinking of different scenarios and how to address them.