You're in luck! After you sell an item to a shop NPC, a new tab labeled "Re-buy" will appear in their store menu. You will be able to purchase the item back from the shop NPC, but only until you log out of the game. When you log out, the "Re-buy" tab will reset.
Shop NPCs can only hold a limited number of items in the "Re-buy" tab. When you exceed that number, items will begin to disappear, earliest first. Therefore, if you sell a lot of items, the earlier items you sold may not be available to buy back.
*Note: Some items may not be available to re-buy.