The Lua manual explains exactly why this works. It describes the index for-loop in terms of a while loop as this:
for v = e1, e2, e3 do block end
--Is equivalent to:
do
local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
if not (var and limit and step) then error() end
while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
local v = var
block
var = var + step
end
end
Notice how the loop variable v is declared inside the scope of the while loop. This is done specifically to allow exactly what you're doing.