There are two reasons this works and you have identified the first one. Namely, the fact that a Pauli operator anywhere in a Clifford circuit is equivalent to a Pauli operator immediately preceding a terminal measurement.
The second reason is the simple predictable effect that a Pauli operator immediately preceding a computational basis measurement has on ...
You are using rz in your code, while the identity you are asking about uses $R_y$
Here is a qiskit code to check the identity:
from qiskit import QuantumCircuit
from qiskit.quantum_info.operators import Operator
from qiskit.visualization import array_to_latex
import numpy as np
circ = QuantumCircuit(1)
circ.ry(-np.pi / 2, 0)
circ.ry(np.pi / 2, 0)
Summary: There is a solution for expressing a tridiagonal matrix of the form you've provided for arbitrary $n$ in terms of Pauli operators using recursion. This procedure is given at the bottom of this answer.
Expressing a tridiagonal matrix recursively
Writing an $n$-qubit tridiagonal matrix in terms of Pauli operators can be done recursively. Ignoring the ...