So just like a 8x8 chessboard has 64 pixels, a 3.1 Megapixel digital camera has a sensor comprising of 2048x1536 = 31,45,728 pixels = 3.1 Megapixels
Pixels can be both square and rectangular as shown below.
Pixels are mostly used as a unit of measure, like 1200 pixels/inch, 640 pixels/line and a space of 10 pixels. We come across these terms so often in Photoshop or CSS.
Number of pixels is often called the resolution in digital imaging but actually it's the maximum resolution a digital camera or computer monitor can achieve if all other factors are compatible.
Resolution ( or Spatial Resolution ) means the no. of close lines that can be resolved in an image or monitor.
Let's again consider a digital camera having 2048x1536 = 3.1 Megapixels. Now the clarity of the image it produces depends on spatial resolution which depends not only on the number of Megapixels but also the size of sensor, lens type etc.
The image it produces can have any resolution less than 1536 pixels/inch
So, don't just get carried away by the no. of Megapixels. A 6 MP camera with a better lens and sensor and if in the right hands can beat an 8 MP one just so easily.