The origin is that people without household ovens used long ago to take their cakes to the local baker to be cooked, along with the cakes of other people. The cakes were marked (patted) by the baker to show the ownership when they all came out of the oven.
wikipedia
Nursery rhyme:
Pat-a-cake, pat-a-cake, baker's man
Bake me a cake as fast as you can
Roll it, pat it, and mark it with a B
Throw it in the oven for Baby and me
Marking pastry or baked goods with an identifiable mark may stem from a time when households without an oven of their own could take their items to a local baker or bake house, paying to have their items finished for a small fee. Marking the pastry would have been a way to ensure the return of the proper item.
This poorly rhyming Wikipedia entry for the nursery rhyme is corrupted from a more reliable (according to my own memory and that of several commentators on my original answer) version as quoted here:
Nursery Rhymes
Pat-a-cake, pat-a-cake, baker's man.
Bake me a cake as fast as you can
Pat it, and prick it, and mark it with "B"
And put it in the oven for Baby and me!
Merriam Webster
pat; patted; patting
transitive verb
to strike lightly with a flat instrument
Hence, an unbaked cake or dough would be patted with an instrument (not strictly flat in the contemporary M-W sense, but we are speaking of an old usage that goes back a long time ago to the days when many houses had no ovens, and the instrument would have made a simple pattern (such as a letter) that made a mark. The patting would have produced a mark in the unbaked item that would have been preserved through the baking process
In the example of the nursery rhyme, the cake is to be marked with "B" for “Baby”
Comments have stimulated me to add to my answer. I have left the wikipedia quotation merely to point out to other readers that it is not quite right. As regards the technicalities of cake, loaf, pastry, rolling, patting, pricking and other constituent aspects, I think we have to remember that a nursery rhyme even when first composed is bound to be a simplification of usages that have then changed over the decades or centuries. The basic point is that a pat with or without an implement will leave a mark that is retained or amplified by the baking and will serve as an identification when the item comes out of the oven.