When you mean Z axis, the other answers make sense. It's close to be perspective. Maybe this can be another step that can be used with the other suggested solutions.
So I'll post this in case the axis is misnamed or you need something that can easily be done with the "free transform" tool in Photoshop or the rotation tool in Illustrator.
Use Free Transform on your shape for Photoshop (command + T). In Illustrator, simply select the rotating tool after selecting your shape.
There is a circle in the middle of the shape now. Drag that circle where you want your shape rotational base point to be (sorry, I don't know how to name this!) On my example below, I moved it on the lower left corner.
Then do your rotation as you normally would.
If you don't want to rotate a shape by the center, that's one trick to do it.