In order to reduce the certification time needed, manufacturers usually manufacture more than one testing aircraft, this way several flight tests can be done in parallel.
Once the airplane is finished, keeping those airplanes is a cost and there is no business case to keep all them. However, an airplane model is a living design and during its lifetime (could be more than 50 years, as B737 for example) the airplane design will be updated.
Things as simple as obsolescence of equipments, small improvements, new technologies... not just re-engining the airplane. Those modifications must be certified as well, and some of the means of compliance require a flight test.
So, in this context:
- Manufacturer usually keeps always one of each airplane model, and likely more than one during a period of time.
- Those having less damages and usable for commercial flights (like long haul testing) are usually sold with a discount (and actually very early...)
- Finally, those that are too expensive to keep and not usable for commercial flight are scrapped or sold/donated to institutions.
Anyhow, is a significant amount of money what is lost, but is part of the development cost. Finally is a business case... you can extend the certification period by 1 year and save 2 testing airplanes? Having 1 year more the manufacturing line stopped waiting for the certification? Finally using several testing airplane is a possitive business case.