The equivalence of two definitions of closed subscheme, Vakil's Ex 8.1.K

Generally in literature, the definition of a closed embedding in the category of scheme is a morphism $\pi:X \rightarrow Y$ between two schemes such that $\pi$ induces a homeomorphism of the underlying topological space of $X$ onto a closed subset of the topological space of $Y$, and the induced map $\pi^\sharp : \mathcal{O}_Y \rightarrow \pi_*\mathcal{O}_X$ of sheaves on $Y$ is surjective.

However Vakil uses a different definition, $\pi:X \rightarrow Y$ is a closed embedding if $\pi$ is an affine morphism and for every affine open subset $\text{Spec}~B \subset Y$ with $\pi^{-1}(\text{Spec}~B)=\text{Spec}~A$, the induced ring homomorphism is surjective, i.e. $B \rightarrow A$ is surjective.

So Ex.8.1.K is to show the equivalence of the two definitions. It is trivial that Vakil's definition implies the definition in literature, but how to show the other direction?

• Let's call your first definition a "H-closed embedding". This definition is local on target by this answer, so the restriction $\pi^{-1}(U) \to U$ is a H-closed embedding for $U = \operatorname{Spec} B$ an affine open in $Y$. By this other answer, we have that $\pi^{-1}(U) \cong \operatorname{Spec} A$ for some ring $A$, and that the map $B \to A$ is surjective. – Takumi Murayama Mar 30 '16 at 23:24

It is clear that the standard definition is local on $Y$, so by taking an affine open cover, it suffices to prove the case where $Y$ is affine, say $Y\cong \operatorname{Spec}(A)$.
We thus have a morphism $\pi: X \rightarrow \operatorname{Spec}(A)$, determined by a ring map $\pi^{\sharp}(A): A \rightarrow \Gamma(X,\mathcal{O}_X)$. Let $I$ be the kernel of the map, so that $\pi$ factors as $\psi \circ \varphi :X\rightarrow\operatorname{Spec}(A/I)\rightarrow\operatorname{Spec}(A)$, where $\psi$ is the standard closed immersion, and $\varphi^{\sharp}$ induces an isomorphism of global sections. We will show that $\varphi$ is an isomorphism.
Since $\pi$ and $\psi$ are both closed immersions in the usual sense, we have e.g. that $\pi_*(\mathcal{O}_X)_{\varphi(p)} \cong \mathcal{O}_{X,p}$ and $\pi_*(\mathcal{O}_X)_q \cong 0$ if $q \not\in \operatorname{Im}(\pi)$. From this observation it follows that $\pi_p:A_{\pi(p)}\rightarrow \mathcal{O}_{X,p}$ is surjective, and so too is $\varphi_{p}:(A/I)_{\varphi(p)}\rightarrow \mathcal{O}_{X,p}$. Provided $\varphi_p$ is also injective, it is an isomorphism, which tells us in particular that $\operatorname{Im}(\varphi) = \operatorname{Spec}(A/I)$, hence $\varphi$ is also a homeormorphism of topological spaces*, and so an isomorphism of schemes.
To prove injectivity, let $\mathcal{I} = \ker(\psi^{\sharp})$ and $\mathcal{J}=\ker(\pi^{\sharp})$. Clearly $\mathcal{I}\subset \mathcal{J}$ and on the distinguished affine open subset $D(f)$, $\mathcal{I}(D(f))= I_f$. $\mathcal{J}(D(f))$ is the kernel of the map $\pi^{\sharp}:A_f\rightarrow \Gamma(\pi^{-1}(D(f)),\mathcal{O}_X)$, and moreover $\pi^{-1}(D(f)) = X_{\pi^{\sharp}(f)}$. They key step now is to notice that $X$ is quasi-compact and quasi-seperated (qcqs), since it is homeomorphic to a closed subset of an affine scheme, and affine schemes are qcqs. The qcqs lemma ($7.3.5$) tells us that there is a natural isomorphism $\Gamma(X,\mathcal{O}_X)_{\pi^{\sharp}(f)} \cong \Gamma(X_{\pi^{\sharp}(f)},\mathcal{O}_X)$. The naturality gives that the ring map $A_f\rightarrow \Gamma(X_{\pi^{\sharp}(f)},\mathcal{O}_X)$ is the localisation of the map $\pi^{\sharp}(A)$, so it's kernel is $\mathcal{J}(\operatorname{Spec}(A))_f = I_f = \mathcal{I}_f$. Thus the two sheaves agree on a base of open sets, and so are equal, hence $\varphi$ is an isomorphism as discussed earlier.
*We knew already that $\varphi$ was a homeomorphism with a closed subset and that $\operatorname{Supp}(\varphi_*(\mathcal{O}_X))=\operatorname{Im}(\varphi)$. But if $\varphi_*(\mathcal{O}_X)\cong \mathcal{O}_{A/I}$, then the sheaves have the same support, namely the whole scheme.