6. Given the set $S=\{1,2,3,4,5,6\}$, a one-to-one mapping $f: S \rightarrow S$ satisfies the condition: for any $x \in S$, $f(f(f(x)))=x$. Then the number of mappings $f$ that satisfy the condition is ( ).
(A) 81
(B) 80
(C) 40
(D) 27
6.A.

It is clear that there does not exist $x \in S$ such that $f(x) \neq x$ and $f(f(x)) = x$. Otherwise, $f(f(f(x))) = f(x) \neq x$, which contradicts the given condition. Therefore, for any $x \in S$, either $f(x) = x$, or $f(x) = x_{1}$, $f(x_{1}) = x_{2}$, $f(x_{2}) = x$, and $x, x_{1}, x_{2}$ are distinct. Hence, there are only the following three cases:
(1) For any $x \in S, f(x) = x$. There is only 1 such $f$;
(2) $f$ contains one cycle: $a \rightarrow b \rightarrow c \rightarrow a$, and the other 3 elements satisfy $f(x) = x$. There are $\mathrm{C}_{6}^{3} \cdot \mathrm{C}_{2}^{1} = 40$ such $f$;
(3) $f$ contains two cycles: $a \rightarrow b \rightarrow c \rightarrow a$ and $x \rightarrow y \rightarrow z \rightarrow x$. There are $\frac{\mathrm{C}_{6}^{3} \cdot \mathrm{C}_{3}^{3}}{2!} \times \mathrm{C}_{2}^{1} \cdot \mathrm{C}_{2}^{1} = 40$ such $f$.
In summary, there are a total of 81 $f$ that satisfy the conditions.