Let $f: X \to Y$ be a closed immersion of locally ringed spaces, that is,
- $f$ is a homeomorphism onto a closed subset of $Y$,
- $f^{\#}:\mathscr{O}_Y \to f_*\mathscr{O}_X$ is surjective,
- $\mathscr{I} = \ker(f^{\#})$ is locally generated by sections (included to agree with Stacks, but probably unneeded for this).
Let $i:Z\to Y $ be the closed space associated with $\mathscr{I}$, that is, $Z = \text{Supp}(\mathscr{O}_Y/\mathscr{I})$ with $\mathscr{O}_Z = f^{-1}(\mathscr{O}_Y/\mathscr{I})$. Stacks claims (proof omitted) (tag 01HO) that there is then a unique isomorphism of locally ringed spaces $f':X\to Z$ such that $f = i\circ f'$.
How can we show this? I think their structure sheaves are isomorphic since they are both cokernels for $\mathscr{I} \to \mathscr{O}_Y$ (although I am not 100% about this) and even with this I do not know how to get the isomorphism at the level of topological spaces. Any help is appreciated!
I claim the image of $f$ is exactly $\operatorname{Supp} (\mathcal{O}_Y/\mathcal{I})$: if $y\notin f(X)$, then for all open $U$ containing $y$ but missing $f(X)$, we have $(f_*\mathcal{O}_X)(U)=0$, so $\mathcal{I}_y = \mathcal{O}_{Y,y}$ which implies $\operatorname{Supp} (\mathcal{O}_Y/\mathcal{I})\subset f(X)$; conversely, the stalk of $f_*\mathcal{O}_X$ at any point $y\in f(X)$ is just the stalk of $\mathcal{O}_X$ at the corresponding point $f^{-1}(y)\in X$, and these stalks are nonzero as $X$ is a locally ringed space. This gives us our isomorphism $f'$ on the level of sets: $f'$ is just the homeomorphism $X\to f(X) = Z$. This is unique because maps of sets are determined by their values and $Z$ is (or ought to be) equipped with the subspace topology.
On the level of sheaves, your logic from your post is enough: both $\mathcal{O}_Y/\mathcal{I}$ and $f_*\mathcal{O}_X$ are cokernels of $\mathcal{I}\to\mathcal{O}_Y$, so they're uniquely isomorphic by the universal property of cokernels.