Sayyiduna Ibn Abbas (may Allah be pleased with them both) reports from the Messenger of Allah (peace and blessings be upon him) that he (peace and blessings be upon him) said: "The first thing Allah, the Mighty and Majestic, created was the Pen, and He commanded it to write everything that was to be." The same is stated in the marfu' hadith of Sayyiduna Ubadah bin al-Samit (may Allah be pleased with him).
Hadith Referenceالسنن الكبرى للبيهقي / كتاب السير / 17704