$\newcommand{\spec}{\operatorname{Spec}}\newcommand{\O}{\mathscr{O}}\newcommand{\V}{\mathbb{V}}\newcommand{\J}{\mathcal{J}}\newcommand{\I}{\mathcal{I}}$Seeing what you want to see unfortunately involves a slightly subtle point where we require $Z$ to be a scheme and use an affine basis. We can't argue purely with the theory of locally ringed spaces. I go about it slightly differently, determining $Z$ as the support $\V(\J)$ of an appropriate sheaf and viewing the calculation $\J=\ker\varphi$ as the main point; I think it is harder to first define $\J=\ker\varphi$ and then try to compute that $\V(\J)=Z$ and identify $\O_Z$ with the quotient by $\J$.
If $\iota:(Z;\O_Z)\hookrightarrow(X;\O_X)$ is a closed immersion of schemes we know $\J=\ker\iota^\#$ is the important thing and - which seems to be the part you didn't get yet - $\V(\J)=Z$, by the general nonsense at the bottom. Suppose $X$ is affine.
Using your nice idea about the spec-global adjunction (valid for locally ringed spaces in general) we can write $\iota$ as the composite: $$\iota:Z\overset{\eta}{\longrightarrow}\spec B\overset{\spec\varphi}{\longrightarrow}\spec A$$Where $A:=\O_X(X),B:=\O_Z(Z)$, $\varphi=\iota^\#_X$ and $\eta$ is the adjunction unit.
We would love to say $\J$ is isomorphic with the sheaf of ideals associated to $\ker\varphi:=\I$. Then $\V(\J)=\V(\I)\cong\spec A/\I$ (using two 'different' notions of $\V$ and $\cong$ is a homeomorphism of spaces) and under this homeomorphism we could easily identify $j^\ast(\O_X/\J)\cong\O_{\spec A/\I}$ since localisation of rings commutes with quotients. Thus $Z\cong\spec A/\I$ as a scheme, underneath $X$, would follow as desired. Make sure you can check $\V(\widetilde{\I})$ - in the sense of the vanishing set of a sheaf of ideals, defined in the bottom section - really is equal to $\V(\I)=\{\mathfrak{p}\in\spec A:\mathfrak{p}\supseteq\I\}$.
To determine $\J\cong\widetilde{\I}$ we just need to check (using the nature of $\eta,\spec\varphi$) that $\I_f$ is the kernel of $A_f\to B_{\varphi(f)}=\O_Z(Z)_{\varphi(f)}\to\O_Z(Z_{\varphi(f)})$ for all $f\in A$. This is $\iota^\#=\eta^\#(\spec\varphi)^\#$'s action on the principal affine open $D(f)$. The exact sequence of $A$-modules $0\to\I\to A\to B$ certainly tells you after localisation that $\I_f$ is the kernel of $A_f\to B_{\varphi(f)}$; thus, the game is to determine that $\O_Z(Z)_{\varphi(f)}\to\O_Z(Z_{\varphi(f)})$ is necessarily injective.
Well, it sure would be an injective map (isomorphism/equality) if $Z$ were affine so here is where we have to use the fact $Z$ locally looks like an affine scheme:
$X$ is compact, so so is $Z$. $Z$ thus admits a finite cover by affine opens $U_1,\cdots,U_n$ (more precisely we require $(U_\bullet;\O_Z|_{U_\bullet})$ is affine). This is the only place where we use the assumption $Z$ is a scheme rather than an arbitrary locally ringed space. We have a commutative diagram, with $g:=\varphi(f)$: $$\require{AMScd}\begin{CD}\O_Z(Z)_g@>>>\O_Z(Z_g)\\@VVV@VVV\\\bigoplus_{k=1}^n\O_Z(U_k)_g@>>\cong>\bigoplus_{k=1}^n\O_Z(U_k\cap Z_g)\end{CD}$$Where the vertical maps are injective by the separatedness property of a sheaf and exactness of localisation (which commutes with finite direct products) and the bottom map is an isomorphism because the $U_\bullet$ are affine.
It follows the top map is injective, as desired, so $\I_f\hookrightarrow A_f$ really is identifiable with $\J=\ker\iota^\#$. For we have just constructed an isomorphism between the two on the basis of principal affine opens $(D(f))_{f\in A}$, a basis for $X$, so by general sheaf theory these sheaves must be isomorphic and we win.
A generic lemma that has nothing to do with schemes per se, just locally ringed spaces: Say $(X;\O_X)$ is a locally ringed space and $\J$ is a sheaf of ideals on $X$. We first claim $(\V(J);j^\ast(\O_X/\J))$ is a locally ringed space, where $\V(\J):=\{x\in X:\J_{X,x}\neq\O_{X,x}\}$, where $j:\V(J)\hookrightarrow X$. Firstly we must convince ourselves $j^\ast$ of a sheaf of rings is again a sheaf of rings in such a way that the unit/counit of adjunction are (local) ring homomorphisms. This is fairly clear. The reason it is a locally ringed sheaf is because each stalk is isomorphic with $\O_{X,x}/\J_{X,x}$ and if $A$ is a local ring and $I\subset A$ a proper ideal (this is the only reason we need to restrict to $\V$) then $A/I$ is also a local ring. Now, say $\iota:(Z;\O_Z)\hookrightarrow(X;\O_X)$ is a closed immersion of locally ringed spaces. If $\J=\ker\iota^\#$ we would want to identify $(Z;\O_Z)\cong(\V(\J);j^\ast(\O_X/\J))$ underneath $X$. First note for $x\in X\setminus Z$ that $(\iota_\ast\O_Z)_x=0$ by closedness, thus $\J_x=\O_{X,x}$. Now if $z\in Z$ then we are given, by the surjectivity condition, that: $$0\to\J_z\to\O_{X,z}\to\O_{Z,z}\to0$$Is exact; as $\O_Z$ is a locally ringed sheaf $\O_{Z,z}\neq0$ and it follows $\J_z$ is a proper ideal of $\O_{X,z}$. Therefore $\V(\J)=Z$ as an exact equality of subspaces of $X$ and $j=\iota$ on the point-set level. Moreover: $$0\to j^\ast\J\to j^\ast\O_X\to\O_Z\to0$$Is an exact sequences of sheaves on $Z$ and $j^\ast$ preserves quotients so $\O_Z\cong j^\ast(\O_X/\J)$ compatibly w.r.t $X$, as desired.