Off the top of my head, the easiest (and maybe the most efficient) way to do this would be to simply mark the item with a players GUID and another unique id of some sort. (you can still display it as a name to players on look ofc). When they type the command to get their item back, simply flag the unique id you gave to the item as "deleted" and give the players item back. You can then check items as players log in or when an item is used, w.e ways you can think of so that if a flagged item exists, it is removed as needed rather than looping through every item in the server, and every offline player all at once upon execution of said command.
Small incomplete example but maybe it will help you understand my concept...
Player gives demon armor to Billy.
Player types command to get it back.
Player gets item back.
Old item is flagged as deleted.
-- Billy was online at the time of the command being used --
- Billy trys to move the demon armor.
- Check if its a deleted item. If yes, remove it and display a message to billy explaining why the item was deleted.
-- Billy already had the item equipped when the command was used and will never need to move it again --
- On global save every hour or w.e it's set to on the server, check all online players equipped items. Remove if they are flagged as deleted.
-- Billy was offline at the time of the command being used --
- Billy logs in.
- Loop through his inventory to see if item exists. Remove if it does.
-- Billy put the item in his depot so the login inventory check couldn't find it --
- Billy clicks on his depot later.
- Loop through depot items to see if item exists. Remove if it does.
-- Billy put the item in his house --
- Any time an item is looked at in a house, it checks if the item is flagged. Remove if it is.
- On server startup (or server save) check house items to see if they are flagged. This may not be needed to prevent abuse but would clean up any "duplicated" items in real time without the need for a player to attempt looking at or moving the item before it's deleted.
As you can see in each of these situations, the player gets the item back instantly and the item is removed the next time it is in some way interacted with.
You are essentially duplicating the item, and adding the old one to a "deletion queue".
The only way an old item can remain in the game is if the item is never interacted with again and if that is the case, then it doesn't matter if the item exists regardless because it is unused.
Keep in mind, this will not work for stackable items and will be more difficult to implement for containers.