First some notation. Say we have $f:M'\to M$ and $g:M\to M''$, and let $f^*:\hom(M, P)\to \hom(M', P)$ and $g^*:\hom(M'', P)\to \hom(M, P)$ be the respective dual maps. Also note that this is something best done with drawings and diagrams (I'd personally argue in favour of a blackboard rather than paper), but we will have to make do with text. I will advise you to draw suitable diagrams for each paragraph to follow along (at least one row with $M'\to M\to M''\to0$, and a $P$ below with relevant $\hom$ arrows into it).
Let's start with showing that $g$ is surjective. Assume for contrapositivity that it is not. Take $P=M''/\operatorname{img}(g)\neq0$, and consider $g^*$. Its kernel contains both the trivial homomorphism and the canonical quotient map, and it is therefore not injective.
Next, let's show that $\operatorname{img}(f)\subseteq \ker(g)$. Or, in other words, the composed map $g\circ f$ is the zero map. Assume for contrapositivity that it is not. Now take $P=M''$ and look at the induced map $f^*\circ g^*$. The image of the identity map $M''\to P$ is exactly the composed map $g\circ f$, which we just assumed is not the zero map. We therefore have $\operatorname{img}(g^*)\not\subseteq \ker(f^*)$.
Finally, we show that $\operatorname{img}(f)\supseteq \ker(g)$. Assume that the $\hom$ sequence is exact for any $P$, and set $P=M/\operatorname{img}(f)$. Since this makes $f^*$ the zero map, $\ker(f^*)\subseteq \operatorname{img}(g^*)$ means that $g^*$ is surjective, which is to say any homomorphism $M\to P$ can be factored through $M''$ as a composition $M\xrightarrow{g}M''\to P$. In particular, this is true for the canonical quotient map. This means that any element in $\ker(g)$ gets sent to $0$ by the quotient map, which by definition of the quotient map means that $\ker(g)\subseteq \operatorname{img}(f)$.