Why do screw thread diameters vary from their nominal values?
Metric screw diameters are always below nominal.
The nominal size is as if the thread came to a sharp point, and it doesn't.  For the exact amount see Wikipedia, from where I lifted this drawing (thanks to user Inductiveload).

Note the P/8 (i.e pitch/8) flat on the outside (top) of the external thread.  This means the outside diameter (roughly doing the trigonometry) is about 0.2×pitch less than nominal.  Taking your M4, with  a pitch of 0.7, you'd expect an OD of 4-0.2*0.7=3.86, an almost perfect match to your 3.88.  Similarly for you M2.5.
So your 3.38mm OD screw isn't M3.  The diameter is good for M3.5, but that has a 0.6mm pitch.  I don't know how you measured the pitch, but that's a fairly easy error.  Without a thread gauge I'd measure the length of 10 (or more) turns, and divide to get the pitch.
M3.5 is rare, but common in British and some European electrical hardware, usually slotted (e.g. it holds our sockets and light switches to the back boxes).  I have seen socket head M3.5 on a metric version of an American product, where a UK engineer would have used M4