In Munkres' Topology there is a theorem which states that the image of a connected space under a continuous map is connected.
In the proof of this, they let $f: X \rightarrow Y$ be continuous, where $X$ is connected, and prove that $Z = f(X)$ is connected. It says that since the restricted map to $Z$ is also continuous, it suffices to consider $g: X \rightarrow Z$. Supposing that $Z = A \cup B$ is a separation, then $g^{-1}(A)$ and $g^{-1}(B)$ are disjoint nonempty open sets whose union is $X$. And then they form a separation of $X$ which is contradicting the fact that $X$ is connected.
So here is my question: The reason we know that $g^{-1}(A)$ and $g^{-1}(B)$ are nonempty is because $g$ is a surjection (according to the book). But how do we know for sure that $g$ is a surjection? We are told that $f$ is continuous and that $g$ is a restriction to $Z$, but what if $f$ is injective, couldn't $g$ be injective as well?