My assumption is that it's so they don't roll away. But if a carpenter spends the majority of his/her day working on a flat surface, a regular pencil wouldn't seem to have the tendency to get away from them.
According to wikipedia, it's so they don't roll away::
A carpenter pencil (carpentry pencil, carpenter's pencil) is a pencil that has a body with a rectangular or elliptical cross-section to prevent it from rolling away. Carpenter pencils are easier to grip than standard pencils, because they have a larger surface area. The non-round core allows thick or thin lines to be drawn by rotating the pencil. Thin lines are required for high precision markings and are easy to erase, but thick markings are needed to mark on rough surfaces. The lead is strong to withstand the stress of marking on such surfaces. The pencil is robust to survive in a construction environment, for example when placed in a bag together with heavy tools. The core is often stronger than in other pencils. Carpenter pencils are also used by builders, because they are suitable for marking on rough surfaces, such as concrete or stone. This shape and lead density aid in marking legible lines with a straight edge that are clear and easy to follow with a saw blade.
Though it's possible that there's some historical precedent involved:
The flat pencil is one of the oldest pencil types. The first versions were made by hollowing out sticks of juniper wood. A superior technique was discovered: two wooden halves were carved with a groove running down them, a plumbago stick placed in one of the grooves, and the two halves then glued together—essentially the same method in use to this day.
A carpenter's pencil has at least two essential characteristics which distinguish it from other types of specialty pencils. First, the shape of a carpenter's pencil prevents it from rolling, even when placed on a steep sloping roof. The most popular shape seems to be a flat, octagonal style. Secondly, the lead must be strong enough to make a heavy mark and to withstand the stresses of carpentry, which frequently requires marking on very coarse surfaces. The leads are usually wide and flat.
Not rolling is only one aspect.
The shape of the pencil also allows the lead to be rectangular. Which allows you to easily draw lines of various thicknesses, simply by rotating the pencil.
The shape allows for a larger volume of "lead", which increases the strength and reduces breaking.
Measuring and spacing
If you know the dimensions of your pencil, you can use it to make quick inaccurate measurements. It can also be used as a spacer, or standoff. The rectangular shape allows for two different dimensions, as opposed to a single one with a round pencil.
Scribing and marking
Scribing and marking can be a bit easier with a rectangular pencil. The rectangular shape also allows you to easily scribe parallel lines, at two different offsets.
I'm sure there's other uses I've missed, as I'm not a professional Carpenter myself.
As a professional carpenter I have used both round and flattened pencils. The most obvious convenience of the flattened pencil is the additional strength created by the flattened piece of graphite. These pencils are usually sharpened to a thin line instead of a point. Since marks are made by moving the pencil lengthwise in the direction of the mark you have at least an eighth inch cross-section of graphite to rub against the rough surface of the wood as compared to the tiny point on a traditional pencil. The flattened surface is also very easy and fast to sharpen with a utility knife which is a tool a carpenter is usually carrying around.
WHen laid flat it can draw a line 1/8 inch parallel to a surface. When laid on its edge in can draw a line 1/4 inch parallel to a surface.
You can also jam a square pencil into The claw of your hammer to scribe hard to reach places I.e if some one was sheeting a curved balcony and wanted to to scribe the shape of the curve on to the plywood from underneath no time needs to be wasted on moving a ladder.