The electronic structure of ${\mathrm{SrTiO}}_{3}$ and ${\mathrm{SrHfO}}_{3}$ (001) surfaces with oxygen vacancies is studied by means of first-principles calculations. We reveal how oxygen vacancies within the first atomic layer of the ${\mathrm{SrTiO}}_{3}$ surface (i) induce a large antiferrodistortive motion of the oxygen octahedra at the surface, (ii) drive localized magnetic moments on the Ti $3d$ orbitals close to the vacancies, and (iii) form a two-dimensional electron gas localized within the first layers. The analysis of the spin texture of this system exhibits a splitting of the energy bands according to the Zeeman interaction, lowering of the Ti $3{d}_{xy}$ level in comparison with ${d}_{xz}$ and ${d}_{yz}$, and also an in-plane precession of the spins. No Rashba-like splitting for the ground state or for the ab initio molecular dynamics trajectory at 400 K is recognized as suggested recently by A. F. Santander-Syro et al. [Nat. Mater. 13, 1085 (2014)]. Instead, a sizable Rashba-like splitting is observed when the Ti atom is replaced by a heavier Hf atom with a much larger spin-orbit interaction. However, we observe the disappearance of the magnetism and the surface two-dimensional electron gas when full structural optimization of the ${\mathrm{SrHfO}}_{3}$ surface is performed. Our results uncover the sensitive interplay of spin-orbit coupling, atomic relaxations, and magnetism when tuning these Sr-based perovskites.