Can a Mac OS X DVD that came with a computer be used to upgrade another computer?
Generally and legally no, since the installer on the DVD is checking for machine specific model identifier for the MacBook Pro that the DVD came with as shown in this workaround .


  Once the file is opened, scroll down about ¾ of a page and you will come across a series of strings named hwbeSupportedMachines.


Now, technically the DVD's that came with Macs included a standard Mac OS 10.x.x Install plus new drivers for hardware specific features for that new model, stuff that would not be known about or included in the 10.x retail install DVD Mac OS X versions (builds) for computers. Which means the OS that came with your Macbook Pro once its installed on the Mac Book Pro should include all it needs to run on the iMac too. You just have to get it there, which you could do via cloning a 10.6 install from the MacBook Pro to the iMac or maybe via installing OS X 10.6 on the iMac via the MacBook Pro using FireWire target disk mode.

Untested Solution if you want go the Upgrade path of 10.5 to 10.8 wihout erasing the iMac


Backup important Data on iMac
Boot the iMac into target disk mode, by holding down the T key while boot the iMac.
Connect iMac to the MacBook Pro via FireWire
Boot the MacBook Pro from its 10.6 Installer DVD
Try to select for installation of OS x 10.6 the hard drive with 10.5 installed on it in the iMac.
Note: This is the untested part, the Restore / Install DVD may not allow upgrades from prior OS X versions.
Boot up the iMac and then upgrade to latest version of OS X 10.6
Install OS X 10.8


Recommended Solution:

This will promote better performance via a clean install to a blank disk thus reducein legacy cruft and other left-overs from old OS X installs and old Applications, etc.

First thing upgrade the MacBook Pro to Mountain Lion, if you have not done so already.

Next back up all your files you want from the iMac, since OS X Lion / Mountain Lion will not be able to upgrade users from Mac OS X 10.5 to 10.8.


Create a boot able Mountain Lion install disk(using a hard drive or usb thumb drive)  


Open the Mac App Store, go to purchases and re-download the Install Mountain Lion Installer.
Create a boot able disk drive by extracting the InstallESD.dmg from it to a external drive or partition. See How to create a bootable, backup Mountain Lion install disk
Connect the external drive to the iMac
Start the iMac while holding down the option key. Select the MTN Lion installer to boot from.
Once the MTN Lion installer loads open Disk Utility and Erase the Hard drive in the iMac
Quit Disk Utility
Install OS X 10.8 by selecting Reinstall Mac OS X