Here is a proof which does not use contradiction.

Let $g$ be a function from $Y$ to $Z$. Suppose that $f:X\to Y$ is surjective and $(g\circ f):X\to Z$ is injective.

Let $y_1,y_2\in Y$ and suppose that $g(y_1)=g(y_2)$. Since $f$ is surjective we can write $y_1=f(x_1)$ and $y_2=f(x_2)$ for some $x_1,x_2\in X$, so $$(g\circ f)(x_1)=g(f(x_1))=g(y_1)=g(y_2)=g(f(x_2))=(g\circ f)(x_2)\ .$$ Since $g\circ f$ is injective $x_1=x_2$ and so $y_1=y_2$. Thus $g$ is injective.


You wrote things in a rather messed up way; you mention something by the name of $f'$ which is left undefined; there is a $b$ which seems to have no relation to $a$. Here is a different organization:

Suppose $g$ is not injective. Then there are $a$ and $b$ in the domain of $g$ such that $g(a)=g(b)$ and $a\neq b$. Now $f$ is surjective, so there are $x$ and $y$ in the domain of $f$ such that $f(x)=a$ and $f(y)=b$. Then $$g(f(x))=g(a)=g(b)=g(f(y))$$ and $x\neq y$, because $f(x)=a\neq b=f(y)$. This is impossible if $g\circ f$ is injective.