The delims parameter delimits the text. So it parses the #$H#$E# into %%a==$H %%b=$E. But there is also no delayed expansion, so my guess is that the for %%b in (1) is essentially ignored. And the set ESC statement is working on the parsed token.
It looks to me like the first FOR is processing the output of the command
"prompt #$H#$E# & echo on & for %%b in (1) do rem"
Maybe I get it now. Because that command turns ECHO ON, the output of that command is (would be) the (new) prompt followed by the "rem". That would be #<backspace>#<esc>#rem and the outer FOR sets the variable ESC to the second token.
The surprising part is that the prompt itself is part of the (would-be) output of "prompt #$H#$E# & echo on & for %%b in (1) do rem".