The image formed in a pinhole camera is inverted because of the rectilinear propagation of light.
Light travels in a straight line. Thus light from the top of the object after passing through the pinhole will reach the bottom of the camera screen and light from the bottom of the object will pass through the pinhole and reach the top of the screen. This will cause an inversion of the image as shown below.