Your question has been answered by Joseph with several references,
but since this nice result has several attractive proofs, let me
try to provide one.
Theorem. If $\mu$ is an ultrafilter on a set $X$ and
$f:X\to X$ has the property that $A\in\mu\leftrightarrow
f^{-1}A\in\mu$, then $f(x)=x$ for $\mu$-almost all $x$.
Proof. Consider the function $f$ as a directed graph, where each
point $x$ has an edge to $f(x)$.
Suppose first that $\mu$ happens to concentrate on the set $A$ of
points lying on a finite cycle. By the axiom of choice, let
$D\subset A$ be a maximal set of non-adjacent points in $A$. So
$D$ contains at least one of $a$, $f(a)$ and $f(f(a))$ for any
$a\in A$. It follows that $A\subset D\cup f^{-1}D\cup f^{-2}D$,
and so one of these sets must be in $\mu$. The main hypothesis
then implies that actually all of these sets are in $\mu$. But
notice that any point $y\in D\cap f^{-1}D$ must have $f(y)=y$,
since otherwise $y$ and $f(y)$ would be adjacent points in $D$. So
$\mu$ concentrates on fixed points of $f$, as desired.
To see that this is the only case, suppose next towards
contradiction that $\mu$ concentrates on the set $B$ of points
that are not yet on a cycle, but whose iterates eventually reach a
cycle. Let $B_0$ be those points in $B$ that reach their cycle
first after an even number of iterations of $f$, and $B_1$ the
points that do so first after an odd number of iterates. These
sets are disjoint, but $f^{-1}B_0\subset B_1$ and
$f^{-1}B_1\subset B_0$, and so actually neither can be in $\mu$,
contradicting $B=B_0\sqcup B_1\in\mu$.
Finally, assume toward contradiction that $\mu$ concentrates on
the set $C$ of points whose iterates are not eventually periodic.
This set is the union of those connected components of the graph
that do not contain a cycle. (Each such component is therefore a
tree.) By the axiom of choice, let $D$ select exactly one point
from each component of $C$. Let $C_0$ be the points in $C$ whose
shortest distance to a point in $D$ has even length, and $C_1$ the
points with odd distance to $D$. Thus, $C=C_0\sqcup C_1$ is a
partition of $C$, and $f^{-1}C_1\subset C_0$ and $f^{-1}C_1\subset
C_0$, since applying $f$ once will change the distance by exactly
one. So again, neither set can be in $\mu$, since either would
force the other also into $\mu$, contradicting that they are
disjoint.
So the only possible case is that $\mu$ concentrates on the fixed
points of $f$. QED
The theorem appears to be first proved by Katetov, Commentations
Math Univ Carolinae 8 (1967), 432-433, with a related result given
by Frolik 1968, and Andreas Blass's dissertation (1970).
You may also be interested in the proof of the theorem written by
Bob Solovay.
Finally, I note that the proof uses the axiom of choice, and I am given to understand that the theorem is not provable in ZF. Perhaps someone else can post an answer explaining how a positive answer to your question is consistent with ZF, even though it is ruled out in ZFC.