Assume wlog that $\ker A$ is spanned by the $p$ first vectors of the canonical basis, and write (by blocks)
$$A=\begin{pmatrix}0&C\\0&D\end{pmatrix},\quad B=\begin{pmatrix}E&F\\G&H\end{pmatrix}$$ where $C,F\in M_{p,n-p}$, $D,H\in M_{n-p}$, $E\in M_p$, and $G\in M_{n-p,p}$.
Then,
$$I_n-AB=\begin{pmatrix}I_p-CG&-CH\\-DG&I_{n-p}-DH\end{pmatrix}.$$
By hypothesis, $\operatorname{im}(I_n-AB)\subset\ker A$, i.e. $-DG=0$ and $I_{n-p}-DH=0$, i.e. $H=D^{-1}$ and $G=0$, so
$$A(I_n-BA)=(I_n-AB)A=\begin{pmatrix}I_p&-CD^{-1}\\0&0\end{pmatrix}\begin{pmatrix}0&C\\0&D\end{pmatrix}=0.$$
Note that we surreptisiouly made use of a theorem specific to square matrices:
$$DH=I\implies HD=I.$$