[proofplan]
We prove both directions directly from the categorical definition of a monomorphism. If $f$ is injective, then equality after composing with $f$ forces equality pointwise before composing with $f$, so the two maps into $X$ are equal. Conversely, if $f$ is monic, then two elements of $X$ with the same image define two maps from a singleton into $X$ whose composites with $f$ agree; monicity forces those singleton maps, and hence the two elements, to be equal.
[/proofplan]
[step:Use injectivity to cancel $f$ after precomposition]
Assume that $f: X \to Y$ is injective. Let $Z$ be a set, and let
\begin{align*}
g: Z &\to X, \\
h: Z &\to X
\end{align*}
be functions such that $f \circ g = f \circ h$ as functions $Z \to Y$.
For every $z \in Z$, equality of the two composite functions gives
\begin{align*}
f(g(z)) = (f \circ g)(z) = (f \circ h)(z) = f(h(z)).
\end{align*}
Since $f$ is injective and $g(z),h(z) \in X$, it follows that $g(z)=h(z)$ for every $z \in Z$. By equality of functions through equality at every point of their common domain, $g=h$. Therefore $f$ is a monomorphism in $\mathrm{Set}$.
[guided]
Assume that $f: X \to Y$ is injective. To prove that $f$ is a monomorphism in $\mathrm{Set}$, we must verify the defining cancellation property: for every set $Z$ and every pair of functions
\begin{align*}
g: Z &\to X, \\
h: Z &\to X,
\end{align*}
if $f \circ g = f \circ h$ as functions $Z \to Y$, then $g=h$ as functions $Z \to X$.
So let $Z$ be a set, and let $g,h: Z \to X$ be functions satisfying $f \circ g = f \circ h$. Equality of functions means equality at every point of the common domain. Thus, for each $z \in Z$,
\begin{align*}
(f \circ g)(z) = (f \circ h)(z).
\end{align*}
Expanding the definition of composition gives
\begin{align*}
f(g(z)) = f(h(z)).
\end{align*}
The elements $g(z)$ and $h(z)$ both lie in $X$, because $g$ and $h$ have codomain $X$. Since $f$ is injective, equality of their images under $f$ implies equality of the elements themselves:
\begin{align*}
g(z)=h(z).
\end{align*}
This holds for every $z \in Z$, so $g$ and $h$ agree pointwise on their common domain. Hence $g=h$. This is exactly the monomorphism condition for $f$ in $\mathrm{Set}$.
[/guided]
[/step]
[step:Use monicity on maps from a singleton to prove injectivity]
Assume that $f: X \to Y$ is a monomorphism in $\mathrm{Set}$. Let $x_0,x_1 \in X$ satisfy $f(x_0)=f(x_1)$. Define the singleton set $S:=\{*\}$, and define functions
\begin{align*}
g: S &\to X, & * &\mapsto x_0, \\
h: S &\to X, & * &\mapsto x_1.
\end{align*}
Then the composites $f \circ g: S \to Y$ and $f \circ h: S \to Y$ satisfy
\begin{align*}
(f \circ g)(*) = f(x_0)=f(x_1)=(f \circ h)(*).
\end{align*}
Since $S$ has exactly one element, this pointwise equality implies $f \circ g=f \circ h$. Because $f$ is monic, $g=h$. Evaluating this equality at $* \in S$ gives
\begin{align*}
x_0 = g(*) = h(*) = x_1.
\end{align*}
Therefore $f$ is injective.
[guided]
Assume that $f: X \to Y$ is a monomorphism in $\mathrm{Set}$. We must prove that $f$ is injective. Thus let $x_0,x_1 \in X$ be arbitrary elements satisfying
\begin{align*}
f(x_0)=f(x_1).
\end{align*}
Our goal is to prove $x_0=x_1$.
The categorical way to compare elements is to view them as maps from a singleton. Define the singleton set $S:=\{*\}$. From the two elements $x_0$ and $x_1$, define two functions
\begin{align*}
g: S &\to X, & * &\mapsto x_0, \\
h: S &\to X, & * &\mapsto x_1.
\end{align*}
These are well-defined functions because specifying the image of the unique element $* \in S$ completely determines a function from $S$.
Now compute the composites with $f$. The maps $f \circ g: S \to Y$ and $f \circ h: S \to Y$ satisfy
\begin{align*}
(f \circ g)(*) = f(g(*)) = f(x_0)
\end{align*}
and
\begin{align*}
(f \circ h)(*) = f(h(*)) = f(x_1).
\end{align*}
By the hypothesis $f(x_0)=f(x_1)$, these two values are equal:
\begin{align*}
(f \circ g)(*)=(f \circ h)(*).
\end{align*}
Since $S$ has no elements other than $*$, equality at $*$ is equality at every point of the domain $S$. Hence
\begin{align*}
f \circ g = f \circ h.
\end{align*}
Because $f$ is monic, the equality of composites implies
\begin{align*}
g=h.
\end{align*}
Evaluating the equality $g=h$ at the element $* \in S$ gives
\begin{align*}
x_0 = g(*) = h(*) = x_1.
\end{align*}
Thus every pair of elements of $X$ with the same image under $f$ is equal, so $f$ is injective.
[/guided]
[/step]
[step:Combine the two implications]
The first step proves that every injective function $f: X \to Y$ is a monomorphism in $\mathrm{Set}$. The second step proves that every monomorphism $f: X \to Y$ in $\mathrm{Set}$ is injective. Therefore $f$ is a monomorphism in $\mathrm{Set}$ if and only if $f$ is injective.
[/step]