First Person Camera Control Issue

Hey guys, noob here. I want to make a free-looking camera from scratch. What I did was that whenever mouseX changed, the camera would rotate along the Y axis, and whenever mouseY moved, the camera would rotate along the X axis. Individually, I am able to do that. But when I put them together, a rotation along the Z axis happens as well.
Any ideas why?

function Update () {

Screen.lockCursor = true;
transform.position = playerPosition.position + Vector3(distanceX, distanceY, distanceZ);
if (Input.GetAxis("Mouse X") < 0) {
	playerPosition.Rotate(Vector3(0, -1, 0) * sensitivityX/4);
	transform.Rotate(Vector3(0, -1, 0) * sensitivityX/4);
}
else if (Input.GetAxis("Mouse X") > 0) {
	playerPosition.Rotate(Vector3(0, 1, 0) * sensitivityX/4);
	transform.Rotate(Vector3(0, 1, 0) * sensitivityX/4);
}

if (Input.GetAxis("Mouse Y") < 0) {
	transform.Rotate(Vector3(1, 0, 0) * sensitivityY/4);
	//previousYPos = transform.rotation.x;
}
else if (Input.GetAxis("Mouse Y") > 0) {
	transform.Rotate(Vector3(-1, 0, 0) * sensitivityY/4);
}

}

hi. unity3d has mouse look script. here is some more scripts in this link find one which you want from that bundle. 1