Nobody wrote down how writing got invented (duh), but there is speculation as to the origin of writing. Archeologists have found small clay objects that they think were used in commerce to represent various goods, starting in about 7000 BC in modern-day Iraq. Much as a dollar bill used to represent 1/32th ounce of gold, they think that a marble-with-a-cross-in-in used to represent one sheep.
Around 3500BC, Sumerians started wrapping the tokens in clay “envelopes”, presumably to aggregate the tokens in a way that would prevent tampering. Joe and Bob could seal the envelope when they made an agreement, then later open the envelope together to verify that Bob had agreed to bring three sheep back from the market for Joe.
Eventually, they started pressing the tokens into the clay envelope before they sealed it, presumably so that they could see what was inside. (This helps Bob remember how many sheep he needs to buy when he gets to market.) It is a small step from there to realize that you don’t need to enclose the tokens if you have their impression in the clay, and a small step from there to realize that they didn’t actually need the tokens at all.
We probably won’t ever have proof that this is how writing started, but I like this story.