Following @wojowu's suggestion, we have:

Let $h$ be the side of the inner cube, and let $(i,j,k)$ be its corner nearest the origin. Then we have $0\le i,j,k < n-h+1 \le n$.

Let us describe our rectangle by the corners $(x_1, y_1)$ and $(x_2, y_2)$ with $x_1<x_2$ and $y_1<y_2$. Then our mapping from $(i,j,k,h)\to (x_1,y_1), (x_2,y_2)$ looks like

$$ (i,j,k,h) \to
\begin{cases}
(i,k),(j,n-h+1) & \text{if $i<j$} \\
(j,k),(n-h+1,n-h+1) & \text{if $i=j$}\\
(k,j),(n-h+1,i) & \text{if $i>j$}.
\end{cases}
$$

Going the other way we have
$$
(x_1,y_1),(x_2,y_2)\to
\begin{cases}
(x_1,x_2,y_1,n-y_2+1) & \text{if $x_2 < y_2$}\\
(x_1,x_1,y_1,n-y_2+1) & \text{if $x_2 = y_2$}\\
(x_2,y_1,x_1,n-y_2+1) & \text{if $x_2 > y_2$}
\end{cases}
$$

It is easy to see that these are inverse.

needsuch a proof? $\endgroup$3more comments