TL;DR. Take any non-abelian group $G$, choose $g \in G \setminus Z(G)$. Then the inclusion map $\langle g \rangle \hookrightarrow G$ doesn't preserve centers.
Surprisingly, it seems this precise question hasn't been asked before on the site, but there are many highly related questions:
Now to add some content here. I will explain how to systematically develop a counterexample, or rather, "all counterexamples". I want to emphasize that there is no need to "come up" with some special groups.
Every homomorphism of groups $f : G \to H$ factors as
$$G \xrightarrow{p} f(G) \xrightarrow{i} H,$$
where $p$ is a surjective homomorphism and $i$ is an injective homomorphism, in fact the inclusion of a subgroup. We know that the claim is true for $p$, and the homomorphisms for which the claim holds are stable under composition. Thefore, the whole problem reduces to the case of the inclusion of a subgroup:
If $H \subseteq G$ is a subgroup, do we have $Z(H) \subseteq Z(G)$? Well, the center is most easy to describe when the group is abelian. When $G$ is abelian, $H$ is also abelian, and the answer is trivially yes. But what if $H$ is abelian? Then we ask ourselves if $H \subseteq Z(G)$ holds for every abelian subgroup $H$ of $G$. But then this holds for every cyclic subgroup in particular (and the converse also holds, since trivially every abelian group is the sum of its cyclic subgroups). So we get $\langle g \rangle \subseteq Z(G)$, which means $g \in Z(G)$.
This means every non-abelian group $G$ provides a counterexample. Take any $g \in G \setminus Z(G)$, and define $H := \langle g \rangle$. Then $Z(H) = H$ is not contained in $Z(G)$. And thus the inclusion homomorphism $i : H \to G$ does not preserve centers.