Let x = the number of pencils.
"If i put 9 pencils into each jar, I will have 2 jars left over(empty)"
That means you have x/9 + 2 jars.
"If I put 6 pencils into each jar then I will have 3 pencils left over."
That means you have (x−3)/6 jars.
Just set each expression equal to each other, since you're talking about the same number of pencils and jars in the problem:
x/9 + 2 = (x−3)/6
(x+18)/9 = (x−3)/6
6(x+18) = 9(x−3)
6x + 108 = 9x − 27
135 = 3x
x = 45
Plugging back into one of the previous equations for jars...
x/9 + 2
= (45)/9 + 2
= 5 + 2
= 7
Therefore you have 45 pencils and 7 jars.