If I am not mistaken the rulebook states that a punt fielded in the endzone and run out that goes dead anywhere short of the 20 yd line is automatically awarded the 20 yd line. A punt that is run out of the endzone is out of the endzone. A punt fielded in the endzone that goes dead in the endzone is a rouge. By re entering the endzone a ball that goes dead is now a safety, therefore I believe that the guarantee of the 20 yd line is inconsistent with the rules and the interpretation should be that a ball fielded in the endzone, run out, run back in and goes dead at the 2 should only get the 2.
The quote is this (3-2-4): "On any kick from scrimmage (which excludes kickoffs) where the ball enters the end zone, the receiving team being able to successfully advance the ball legally out of the end zone and retain possession, may choose to scrimmage at their own 20-yard line or where the ball becomes dead."
Thus, to me, the relevant points are:
1. Is this a kick from scrimmage?
2. Did the kick go into the end zone?
3. Did the receiving team successfully - and legally - advance the ball out?
4. Did they retain possession?
If Team B catches the ball 5 yards deep, gets it out to the 1, retreats back into the end zone and gets back out to the 2, the answer to all three questions is "yes". So, they get the ball at the 20.
Had the returner been tackled in the end zone, the answer to Question 3 would have been no, and a safety touch would be given.
It is irrelevant how the returner decides to get to the 2. The bottom line is that he got there. Yes, he puts himself at risk of giving up a safety by retreating into the end zone, but I see nothing in the book that says he gives up the guarantee of the 20 yard line.