Only bijective mappings are invertible. Clarifying proof. 
Suppose that $f : A \mapsto B$ is invertible with inverse $g : B \mapsto A$. Then $g \circ f = \operatorname{id}_A$ which means $\forall a \in A : g(f(a)) = a$. Let's take some $a_1, a_2 \in A$ with $f(a_1) = f(a_2)$; then $a_1 = g(f(a_1)) = g(f(a_2)) = a_2$.

The thing which I can't get is $g(f(a_1)) = g(f(a_2))$ equality. Where it comes form? Clearly, $a_1 = g(f(a_1))$ and $a_2 = g(f(a_2))$, but why it implies $a_1 = a_2$? Why can't they be non-equal, i.e. different, but mapped to the same $b = f(a_1) = f(a_2)$? 
The only assumption I came up with is that having $b = f(a_1) = f(a_2)$ seems to violate mapping requirement for $g : B \mapsto A$, since it "maps" exactly the same $b$ twice: $(b, a_1)$ and $(b, a_2)$... nevertheless, still having a feeling like I do miss something important. Do I? 
 A: This is a property of equality. Whenever we have an equality $\alpha=\beta$, then any true statement with all or only some occurrences of $\alpha$ replaced with $\beta$ remains true. For example, we have the equality $2+3=5$. Therefore, replacing $5$ with $2+3$ in the true statement $2\cdot 5>5+4$ produces the true statement $2\cdot (2+3)>5+4$ (note that "syntactic sugar" in form of parentheses had to be added to avoid producing $2\cdot 2+3>5+4$, where we multiply the first $2$ not with $2+3$, but only with $2$).
Another important property of equality is that every object is equal to itself.
Thus certainly,
$$\tag1 g(f(a_1))=g(f(a_1)).$$
Now we take the assumed equality 
$$\tag2f(a_1)=f(a_2) $$
and replace on occurrence of $f(a_1)$ with $f(a_2)$ in $(1)$ accordingly:
$$ g(f(a_1))=g(f(a_2)),$$
et voila.
A: You are assuming that $f(a_1)=f(a_2)$. But then $g\bigl(f(a_1)\bigr)=g\bigl(f(a_2)\bigr)$, which means that $a_1=a_2$, since $g\circ f=\operatorname{id}_A$, wich means that$$(\forall a\in A):g\bigl(f(a)\bigr)=a.$$
A: You've defined $a_1$ and $a_2$ so that $f(a_1)=f(a_2)$. Now, 
\begin{align}a_1=g(\underbrace{f(a_1)}_{=f(a_2)})=g(f(a_2))=a_2.
\end{align}
This works because $g$ is defined as the inverse of $f$, so $g \circ f= \operatorname{id}_A$ is the identity function, and hence $g\circ f(a)=g(f(a))=a$ for any $a \in A$.
You are correct in stating that $g$ would not be a function if $a_1$ and $a_2$ were not equal, since this would mean that $g$ maps the same input, $f(a_1)=f(a_2)$ to two different outputs, $a_1$ and $a_2$. 
A: No, you got it - a function maps an element to only one element, not two, so $g$ cannot simultaneously map $f(a_1)=f(a_2)=b$ to two unequal elements $a_1$ and $a_2$.
A: Since $f(a_1)=f(a_2)$, they are the thing, we can apply $g$ and their output should be the same.
hence $$g(f(a_1))=g(f(a_2))$$
but we have $g(f(x))=id_A(x)=x$,
Hence $a_1=g(f(a_1))$ and $g(f(a_2))=a_2$.
but we know that $g(f(a_1)) = g(f(a_2))$, hence $a_1=g(f(a_1))=g(f(a_2))=a_2$.
Remark:
Nothing wrong with writing $b=f(a_1)=f(a_2)$, we didn't say that $a_1$ and $a_2$ are distinct.
A: If $f(a_1)=f(a_2)$, necessarily $\;g\bigl(f(a_1)\bigr)=g\bigl(f(a_2)\bigr)$.
And as $g\bigl(f(a_i)\bigr)=a_i$, this is the same as $a_1=a_2$.
