Stephan is correct, you need to use enabledlayedexpansion when using a nested variable. Here's your code with that syntax (replacing % with ! when using such variables):
setlocal enabledelayedexpansion
if not exist "%~n1.ext" (
set /P z="PROMPT (y,n)?"
if /i "!z!" == "y" (
echo if is working
)
)