Surjective map on coordinate rings implies the map is injective

Let $X$ and $Y$ be affine varieties and $f: X \rightarrow Y$ a polynomial map. If the induced map on coordinate rings $K[Y] \rightarrow K[X]$ is surjective why this implies that $f$ is injective?

-

If $x\neq x'\in X$, then there exists $\phi\in K[X]$ with $\phi(x)=0$ and $\phi(x')=1$.
Let $\phi= f^\ast \psi=\psi\circ f$ for some $\psi \in K[Y]$ (surjectivity hypothesis).
Then $\psi( f(x))=(f^\ast \psi)(x)=\phi(x)=0$ whereas $\psi( f(x'))=(f^\ast \psi)(x')=\phi(x')=1$, and of course the inequality $\psi( f(x))\neq \psi( f(x'))$ forces $f(x)\neq f(x')$.

Edit (answer to Heitor's question in comment)
If the ring morphism $f^\ast:K[Y] \rightarrow K[X]$ is surjective then the morphism of varieties $f:X\to Y$ is closed and in particular $f(X)\subset Y$ is algebraic.
However just assuming that a morphism of affine varieties $g:X\to Y$ is injective does not guarantee that $g(X)$ is closed.
A simple counter-example is the open immersion $X=\mathbb A^1\setminus \{0\}\hookrightarrow Y=\mathbb A^1$ whose image $X$ is clearly not closed in $Y$.

[Reminder: $X=\mathbb A^1\setminus \{0\}$ is affine with function ring equal to $K[T,T^{-1}]$.
More geometrically the variety $X$ is isomorphic to the affine hyperbola $xy=1$ in the plane $\mathbb A^2$]

-
why such $\phi$ exists? – user31509 May 20 '12 at 8:01
Since $X$ is affine, we have $X\subset A^n$ for some $n$. Since $x=(a_1,...,a_n) \neq x'=(a'_1,...,a'_n)$, some coordinate of $\mathbb A^n$ is different for those two points: say $a_1\neq a'_1$. You may then take $\phi(t_1,...,t_n)=\frac {t_1-a_1}{a'_1-a_1}$ – Georges Elencwajg May 20 '12 at 8:51
@GeorgesElencwajg. I was wondering: is this enough (I mean $f$ injective) to conclude that $f$ is then closed, i.e. $f(X)$ is algebraic ? – Heitor Fontana May 31 '13 at 11:29
Dear @Heitor, I have written a detailed answer to your question in an Edit. – Georges Elencwajg May 31 '13 at 11:58
@GeorgesElencwajg: If both $x,x^{\prime}\in X$ and $X$ is an affine variety (irreducible algebraic set) then why only $x$ is the zero of $\phi$ while $x^{\prime}$ is not? – H.E Feb 3 '14 at 15:52

I'll assume you understand the correspondence between maximal ideals of $K[X]$ and points of $X$. I'll also assume the base field $k$ is algebraically closed. Then, for every point $p$ of $X$, there is a unique $k$-algebra homomorphism $\epsilon_p : K[X] \to k$ corresponding to $p$. We compose this with the pullback map $f^* : K[Y] \to K[X]$ to $\epsilon_{f (p)} : K[Y] \to k$. Now, suppose $f (p) = f (p')$. Then $\epsilon_{f (p)} = \epsilon_{f (p')}$, but then that means $\epsilon_p \circ f^* = \epsilon_{p'} \circ f^*$, and hence, $\epsilon_p = \epsilon_{p'}$ if $f^*$ is surjective. But that implies $p = p'$, so $f$ is injective.

-