We report a global structural distortion in Sr2IrO4 using spatially resolved optical second and third harmonic generation rotational anisotropy measurements. A symmetry lowering from an I41/acd to I41/a space group is observed both above and below the Néel temperature that arises from a staggered tetragonal distortion of the oxygen octahedra. By studying an effective superexchange Hamiltonian that accounts for this lowered symmetry, we find that perfect locking between the octahedral rotation and magnetic moment canting angles can persist even in the presence of large noncubic local distortions. Our results explain the origin of the forbidden Bragg peaks recently observed in neutron diffraction experiments and reconcile the observations of strong tetragonal distortion and perfect magnetoelastic locking in Sr2IrO4