There are many scenarios where players will be confined for a period of time in a location: Prison, castle under siege, mega dungeon adventure (like Undermountain) or a labyrinth. I'd like to play the Renraku Arcology Shutdown scenario of Shadowrun (but in 2072) and I don't want them to be able to simply blow a window and escape but I don't want them to feel railroaded and whatever they do they won't be able to simply walk out. Of course the party goal will be to exit so how can I let them do that but over at least a couple of game sessions?
It's OK to confine your players, and it's OK to have in place things that will thwart the easy outs that would be boring for everyone involved. The point where it will start feeling like railroading is when either A) The reasons why escape methods X, Y, and Z don't work don't make sense or B) The rules keep changing, specifically to thwart ideas that would in fact work otherwise.
Now, this doesn't mean that you have to explain every facet of your reasons in case A, or that you can't adjust on the fly sections you haven't fully defined. It's a matter of player perception. By placing the players in this confinement and making leaving a goal, you've presented your players with a challenge. Like any combat or social encounter there are knowns to factor in and unknowns to discover, but the players will expect that the same social contract that applies in the previous two applies here.
The key lies in not punishing players for solving the challenge in the "wrong" way. If you have a reason you want them in this confinement for a period of time then relying only on escape is a dangerous proposition. I'd give them some interesting sub-goals to discover while inside so that it's the players' choice to stick around, and not some feeling of arbitrary content consumption time. Either way just in case I'd prepare to handle a situation where the players escape and choose not to follow any of the hooks.
All in all, make it as confining and effective as you like. So long as to the players' view it stays internally consistent and non-arbitrary, and interesting beyond just waiting for the GM to let you out, you should be fine.
(Note that as with all guidelines, selectively breaking them can be done effectively, it just takes work. For instance, the prison of a reality warper is unlikely to stay internally consistent)
Interesting things to do
Any place with interesting things for characters to do is a good place for adventure. This can include people to interact with, things to make, and information to share.
Choices to make
All the interesting feels railroaded if there's only one path. Therefore, let the players make choices.
Likewise, if the choices all lead the same place, players tend to catch on... and then get upset. So, make certain the choices are meaningful.
This means also not letting the players notice the rails.
If they want to hide in their cells for a week, fine, let them. Narrate the routine they notice.
Likewise, if work is a major part of that prison's environment, no need to dwell on the work. Roll for interesting discussion partners, but generally, reduce work to, "you spend the next 8 hours hacking at the mine walls with picks."
The list isn't exhaustive
As a GM, you are not going to have a complete lis of every way in or out, nor every possible circumvention of the defenses.
So, if they come up with something new, run with it. If it makes sense, let it work.
Confinement is Normal
Classic D&D Dungeons are a clear case of a railroad with choices that matter... each hall is a chunk of rail, and each room and junction a meaningful choice.
Give them a reason to get out (or stay in)
If you want them to engage with the prison itself, give them either a strong reason to leave, or a strong reason to stay and play within.
But, by the same token, if they want to just do the time and get out, don't take it personally... resolve a few "encounters" per month, and let it go by.
Give them clues & Multiple ways out.
If you want them to engage in an attempt to escape, you need to provide the clues for at least two different methods out. Let the players work on a plan. Let them slowly build a reasonable map. Give them the tools to make it out alive, but also let them know what the hazards are.
And whatever you do, if they try the brute force method, remember that it's Okay to kill PC's who do stupid.
Further, remember: a well clued set of 2+ ways to escape means at least one meaningful choice: which way to go. Make certain that the various ways have different risk levels.
Have a reason they're in there
Confinement as a deus ex machina event is bad. Confinement that makes story sense is not bad, but might not be good, either.
Confinement for reasons the players can see coming really does make the confinement itself more enjoyable.
Even if the reason they're there is wrong, it needs to be plausible. So, if locked up for a murder they didn't commit, it needs to have been plausible that they could have...
... unless it's an obvious frame-job. But then the frame is the reason they're in, and it needs to have a reason.
Here are the big issues I see with getting locked into a prison in a game like DnD or Pathfinder:
*The editorial you here; I am not accusing the OP of anything!
Make sure they don't have obvious easy tools to escape. In this case, do not let them have rope on them and make sure they are too high in the air to take the fall.
Try to consider all the obvious ways to escape the situation with ease. Eliminate those somehow. If they find a way around your plans. YAY! They are smart and should be rewarded.
Make the environment just outside of the prison incredibly inhospitable. The goal wouldn't be to escape the building so much as it would be to secure transport/get to a teleporter/however the prison is accessed. Then, they can leave the prison however they find a way - the building at least. It could even be a part of the adventure, fights on the rooftops and all that.
If escape is the first priority, count on them to find a way around a great many obstacles, in which you'll have the choice but to limit their creativity (read here as railroad) on the fly - if you want to stop them from escaping. However, if escape is secondary to acquiring gas masks from the guards' locker room or accessing a central control panel to unlock the route to the necessary seafaring ships, escape becomes secondary to the primary concern of getting the means of escape.
By complicating their escape via adding prerequisites to the escape plan, you not only give room for a couple sessions but also make their escape that much more spectacular. "We jumped the collapsing bridge as guards waylaid us with gunfire" appeals more than "after we tripped the alarm there was enough chaos for us to slip through the service door," at least to my players and I. Then it doesn't have to be railroading. If they find a way around the requirement then they did, but at least it won't be as simple as kicking down the right door (hopefully).
In D&D, I once constructed a 'reverse dungeon' where players are captured in an ambush by an orc kidnapping ring and inserted into a dungeon at their lair's lowest level. The idea was for players to improvise their own escape from the cell block and move up the dungeon levels ultimately reacquiring their possessions, gaining new treasure, rescuing fellow prisoners, and gaining revenge.
This was a very detailed, and well planned adventure, particularly the cell block. There were conceivably many ways to escape. The individual cells all contained unique exploitable weaknesses, the orc guards were on the edge of mutiny, fellow NPC prisoners could be organized, shivs and other weapons could be made, and PC's had a chance to hide some of their smaller items before capture. I thought it was key that all PC's had a lot to do (not just thieves) AND that there was more than 1 correct answer to the question of escape.
What Didn't Work
I ran this adventure three times. Two groups loved it, the third despised it - when they were captured, some players actually stood up and walked out of the room (a first for me). There were two issues.
As mentioned by John Craven above, the players hated the ambush nature of being captured, feeling railroaded. An improvement would be to present the PC's with a high risk choice to make with a big reward. If they failed, then they were captured. This would make players feel more in control of their fate, even if it was a bad fate. GM's need to put as much thinking into how to run the PC's capture as their confinement.
The second issue was that the third player group was just never gonna dig this kind of adventure anyway. That group was pretty tame and uncreative. Every time my campaign presented them with something other than - open door, kill monster, collect treasure, open next door - they'd complain. The point here is know thy players. If you want to try something new, make sure your group wants something new.