Why is earth's axis tilted?
The simple answer: Because it is.

There's a nice theory that explains why the rotation axis of a big gas giant is more or less coaligned with its orbital angular momentum vector. Those big gas giants form somewhat like a snowball rolling downhill becomes bigger and bigger and bigger. This doesn't apply to rocky planets, let alone lesser gas giants and ice giants. Saturn's rotation axis is inclined by 27°, Uranus by 98°, Neptune by 30°. It's a bit random.

With regard to the rocky planets, whatever rotation Mercury had in the distant past is long lost. Tidal interactions with the Sun will necessarily have locked Mercury into a resonance between rotation and orbital rotations. That the resonance is 3:2 rather than 1:1 is a result of the largish eccentricity of Mercury's orbit.

Mars is not tidally locked, but it's obliquity is notoriously chaotic. A number of past and future recreations of Mars' rotation show chaotic behavior. Whatever rotation Mars had when it formed has long been lost thanks to that chaos. The obliquity of Mars has gone through huge changes over the last few billion years, occasionally changing rapidly, occasionally remaining stable. That's the nature of chaos.

Venus also isn't tidally locked per se, but essentially it is. The rotation of Venus is rather bizarre. The solid part of the planet rotates retrograde at a very slow rate. On the other hand, the upper parts of Venus' thick atmosphere rotate prograde. The combined rotation represents one of the four final states in which Venus as a whole can rotate (multiple articles by Laskar, and by others). Venys is in its final rotational state.

The Earth is a marked exception amongst the rocky planets. The key to that marked exception is our exceptionally massive moon. The Earth's moon acts to stabilize the Earth's rotation. Whatever obliquity the proto-Earth had prior to the giant impact is long lost. On the other hand, the obliquity after that giant impact has remained fairly constant thanks to that stabilizing influence.