how does this work?

This is one of the first things we study in Number Theory. Its a counterintuitive result of residue systems in a congruence class, it works for 2,3 any mutiple of them. For the actual proof you need a few theorems that deal with Complete Residue Systems and Reduced Residue Systems modulo any integer.

Modulo 9 the CRS={0,1,2,3,4,5,6,7,8} and the RRS={1,2,4,5,7,8}. Since every integer can be expressed as one of {3m,3m+1,3m+2} where m is some integer, we will always produce a member of the RRS for any integer. There are a few ways to do this next part:1.) we can show through exhausting every case that for any integer modulo 9 we will subtract its congruence when we add its representative integers
2.)We can prove this as a generality for cyclic groups with generators.

If you think this is long, the real proof takes a few pages…