We can reduce to the case where $A$ and $B$ are commutative subalgebras, e.g. taking the closed star-algebra generated by $a$. Then $A = C(X)$ and $B = C(Y)$ for compact Hausdorff spaces $X$ and $Y$, and we have a continuous linear map $f : A \to B$. From this we get a continuous map $f^*$ between the duals $C(Y)^*$ and $C(X)^*$ in the weak* topology.
We also have natural embeddings $X \to C(X)^*$ and $Y \to C(Y)^*$ coming from point evaluations (i.e. delta-masses, in terms of measures). You can recognize the measures supported at a single point by the property that, if $\phi(ab) = 0$, then $\phi(a)=0$ or $\phi(b)=0$. By the condition $f(a)f(b)=0$ whenever $ab=0$, it follows that $f^*$ sends delta masses to multiples of delta masses; and from $f(1)=1$ we get that $f^*$ sends $Y$ continuously to $X$.
Thus $f: A \to B$ is a morphism of star-algebras, induced by a continuous map from $Y$ to $X$. In particular $f(a^2) = f(a)^2$.