The first version of your sentence isn't ungrammatical. But it is very confusing, and so a skilled native speaker would likely avoid it. The confusion (for me) happens at the "from", right after "$25". I initially thought that $25 was the sale price, not the (past) purchase price.
The cause of the confusion is that you're trying to attach several subordinate clauses on to "bought". But that verb is already part of a subordinate clause to "sold" which is the main verb in the sentence, and it's not clear on first reading where the "bought" clause is supposed to end.
I think your intended parsing is:
(I sold (the ring I bought (for $25) (from one of my friends) (a few years ago)) for $5)
All those clauses in the middle are about the purchase of the ring, not about its sale. That's confusing, since the sentence started out talking about a sale, and so the listener is going to be eagerly looking for you to get back to that main topic. When you keep adding clauses about the purchase, they'll keep trying and failing to attach them to the sale, and when that doesn't work they may lose track of what's going on. One inner subordinate clause would have been fine (though if it was only the price, it would still be a bit ambiguous). Three is definitely too much!
Indeed, like me, many readers may succeed in attaching the clause about the $25 price to the sale rather than the purchase, and then be very confused when the next clause only makes sense attached to the purchase. My initial parsing of the sentence went like this:
(I sold (the ring I purchased) (for $25) ...)
But this breaks all of the sudden when we come to "from one of my friends" which doesn't make any sense attached to the sale.
This is a simple sort of garden path sentence, where an initial assumption about a sentence's structure needs to be revised, but only after you've gone beyond the ambiguous section (the ambiguity "led you up the garden path", an obscure idiom that means to mislead somebody).
Adding a comma near the end of the sentence doesn't fix the problem, as the confusion has already happened long before we get to that point. The second version of your sentence that moves the $5 sale price near the start is much clearer, as all the sale information is together. But I think you're still trying to include too much information in a single sentence. It would be much clearer if some of it was separated. I'd prefer making it into two sentences, one about the sale and another about the original purchase, so the different clauses are entirely clear on what they attach to. I'd probably write:
I just sold a ring for $5. I bought it a few years ago from one of my friends for $25.
Or if you want to end with the sales price, put all of the purchase information up front before you bring up the sale at all:
A few years ago, I bought a ring from one of my friends for $25. I just sold it for $5.