On the source screen, you place a flag warp, and on the destination screen, you place a regular warp to guide the player back upon exiting the area.
So, you have your world, with a special screen. Example:
[ j ][ ][ f ][ ][ s ]
[ d ]
j = juni, f = flagwarp, s = shift.
You start at the left screen. You go 2 screens to the right and find a screen with a well, but its closed with somekind of electronic door. You go 2 screens to the right to find the switch.
This shift could do 2 things. Either it sets a flag or it teleports you to a copy of the world where everything you want changed is changed. If you design your level well, you can use this world/copy at most cases, because usually you don't need to go all the way back, so you can design your level so that won't be possible anymore. But your question is about flags, so lets tell you about the alternative.
So, at screen s, you set the switch, which shifts you to the same screen and sets a flag to the state on. Now you move back 2 screens to the left.
On this screen, you place your flag warp. Due to this being here, and you set that a flag must be evaluated, you can move the player to another screen which has the well open. In my example, that is screen d.
Screen D is a copy of screen f, with the exception that you remove the flagwarp and replace it for a warp, and of course, here the well is open.
You may wonder why to put a warp on this screen, well if the player decides to jump over the well or moves back, they should be warped back to the world.
So: f has a flag warp, d has a normal warp.