Michael Quinion explains:
The most probable solution—though unprovable because slang is so rarely written down—is that it comes from London street slang for money. This may well derive from Romany posh, half, originally applied to a halfpenny, then to any small sum of money, and then to money in general. This is recorded from as early as 1830 and was certainly still around in 1892 when Montagu Williams published his Down East and Up West, quoting in it a comment from a Londoner about a street singer who chatted up potential givers of money: “That sort of patter I was just speaking of is the thing to get the posh, they’ll tell you”. A shift in sense from “money”, to “well off”, and hence to “upper-class” is not too hard to imagine.
There is a more direct London slang sense of “dandy”, known at least from the 1890s, which is probably where George and Weedon Grossmith got the name of their character. This might be connected, or it might be a different word altogether.
Whatever its source, it looks from the evidence that posh in the modern sense was at first a military slang term of the First World War. Its first appearance is in the magazine Punch in September 1918, in which an RAF officer is saying to his mother, “Oh, yes, Mater, we had a posh time of it down there”; the verbal phrase to posh up, to make oneself smart, is of the same period.