which of the following is necessarily true for a function $f : X \rightarrow Y$?
1) If $ f $ is injective it means that you can get the input back given the output, which means the existence of $ g $ giving or the identity by composing the other way around.
Note that this is not a proof that what you wrote is wrong, but it shows you the correct version.
2) $ f $ being surjective means that for every possible output $ y $ you can choose (axiom of Choice as mentioned in the comments) a preimage and call it $ g (y) $. Then indeed $ f (g (y))=y$ by construction.