A gain or loss on sale is recorded when there is a difference between carrying value of the asset and a purchase price.
Example:
Asset was purchased by XYZ Corp. for $50,000. After two years of depreciation the value of the asset is $30,000. During year 3 another, company got very interested in the asset and purchased it for $35,000.
From the information given above we can deduce that accumulated depreciation has a balance of $20,000 (Asset purchase $50,000 Less its current carrying value of $30,000).
The following entry is made to record a sale:
Dr. Cash (or accounts receivable) $35,000 (To record proceeds from the sale)
Dr. Accumulated depreciation $20,000 (To remove accumulated depreciation for this asset)
Cr. Asset $50,000 (To remove the asset from the balance sheet)
Cr. Gain on sale $5,000 (To record gain on transaction. It is an income statement line item)