The "to" isn't makin an infinitive, it's showing a goal. It's not like the "to" in "to swim"; instead, it's like the to in "go to Beijing". The goal of the trick is the act of getting out of the well.
It's pretty rare to see "to Xing", but there are a few other phrases that use it. One of the most common is "look forward to Xing", as in "I look forward to meeting you". I am looking forward in time, in the direction of our eventual meeting.
I hope that helps.