This isn't true, take $\displaystyle \cdot ^2:\mathbb{R}\rightarrow[0,\infty)$ and $\displaystyle \sqrt{}:[0,\infty)\rightarrow [0,\infty)$.
Notice that $\displaystyle \sqrt{} \circ \cdot^2$ is not injective.
As for the problem assume $\displaystyle f(x)=f(y)$ then $\displaystyle g(f(x))=g(f(y)) $which implies $\displaystyle x=y$ by the injectivity of $\displaystyle g\circ f$