In BGII, sometimes when you put items in a container, that item (or item stack) will be stored on a separate stack within the container. And, sometimes, when you try to remove items from that stack, they are removed successfully but reappear when you reopen the container. You have to remove the original stack completely first before you can remove items from the duplicate stack permanently.
Making the second stack for similar items is not a bug. This happens when the item's flags or other values are different then the item that is already in there (not identified, stolen / fenced, not pickpocketable, more / less charges).
Not being able to permanently remove items from the second stack is the bug. Based on my observations, this error seems to be tied to the flag that controls whether or not an item is pickpocketable.
Thus, if you put 40 arrows into your bag and then inserted 20 arrows you looted off some enemy that have the "not pickpocketable" flag, you would have one stack of 40 arrows and one separate stack of 20 arrows. If you remove arrows from the second stack, they will reappear next time you open the bag. To remove the 20 arrow stack, you must remove the 40 arrow stack completely first.
My guess that a quick fix would be to remove the "not pickpocketable" flag from items once dropped / picked up.
Assuming the developers ever see this stuff, I hope that helps.