My attempt : Let $f(x_1)=f(x_2)$ for some $x_1$ and $x_2$.
$g\circ f(x_1)=g\circ f(x_2)$ implies that $x_1=x_2$ holds. Thus $f(x_1)=f(x_2)$ implies that $x_1=x_2$ and therefore $f$ is injective What I am not able to understand is that is it necessary for $g$ to be injective? Can someone explain it with mapping diagrams instead of giving examples?
Here you go:
The red lines are to illustrate the necessity of $f$ being injective. Note that, as soon as $f$ sends two points in $A$ to the same point in $B$, there's no separating them out again once $g$ maps them to $C$. So, as the result claims, $f$ must definitely be injective.
The blue lines are to illustrate why $g$ may not be injective. We can have $g$ send points outside of $f(A)$ to wherever we want (including to points that would break injectivity), so long as $g$ remains injective when restricted to $f(A)$.