Legend behind Vastu is described in Chapter 252 of Matsya Purana. Lord Matsya narrated the story to Vaivasvara Manu.
When Lord Shiva was fighting with Andhakasura, sweat drop fell from Lord Shiva's forehead which gave rise to new creature which wanted to devour the three worlds.
तदिदानीं प्रवक्ष्यामि वास्तुशास्त्रमनुत्तमम्। पुरान्धकवधे घोरे
ललाटस्वेदसलिलमपतद्भुवि भीषणम्। करालवदनं
वसुन्धराम्। ततोन्धकानां रुधिरमपिबत्पतितं क्षितौ॥७॥
सर्व पतितं यन्महीतले। तथापि तृप्तिमगमन्न तद्भूतं यदा तदा॥८॥
सदाशिवस्य पुरतस्तपश्चक्रे सुदारुणम्। क्षुधाविष्टं तु तद्भूतमाहर्तुं
ततः कालेन सन्तुष्टो भैरवस्तस्य चाह वै। वरं वृणीष्व
भद्र ते यदभीष्ट तवानघ। १०॥
In days gone by, perspiration (water) trickled from the forehead of
Siva at the time of His fighting fiercely with the demon Andhaka in
course of which the latter was killed; and out of this sweat of the
fatigued Siva was born an attendant, grim in appearance, who looked as
if to swallow the whole universe with seven islands and the sky. He
then began to drink the blood of the Andhaka demons that lay scattered
on the ground; but he was not satisfied with it. Then that hungry
attendant began to practise tapasya with the object of devouring the
three regions, in honour of Siva. The Lord Siva in due time was highly
pleased with the devotee and asked him to select a boon.
Lord Shiva granted the boon to devour the three worlds. The devotee soon conquered the three worlds and Devas, Brahma, Rakshasa and Shiva Ganas collectively captured him and made him motionless.
तमुवाच ततो भूतं त्रैलोक्यग्रसनक्षमम्। भवामि देवदेवेश तथेत्युत्तं च
ततस्तत् त्रिदिवं सर्वं भूमण्डलमशेषतः।
स्वदेहेनान्तरिक्षं च रुन्धानं प्रपतद्भुवि॥ १२॥
देवैब्रह्मणा चाथ शूलिना। दानवासुररक्षोभिरवष्टब्धं समन्ततः॥ १३॥
येन यत्रैव चाक्रान्तं स तत्रैवावसत्पुनः। निवासात्सर्वदेवानां
The devotee said- "O Lord! be pleased to permit me to eat of the three
realms' and the Lord said- "Be it so.' Then that devotee besieged and
brought all the three regions, under his clutches and then fell down
on this earth. The terrified Devas, Brahmā, Ŝiva, demons, Raksasas
got round and captured him from all sides; the being, thus imprisoned,
remained there and since then, owing to all the Devas remaining there
and living round him, he came to be recognised as Vastu Deva.
Vastu Deva then pleaded them to make him free. Devas said that he would enjoy sacrificial offerings of house dwellers and follow certain rules before constructing houses.
अवष्टढधाश्च तेनापि विज्ञप्ताः सर्वदेवता। प्रसीदध्वं सुराः सर्वे
स्थास्याम्यहं किमाकारो हावष्टत्यो ह्यधोमुख:। ततो ब्रह्मादिभिः प्रोक्ततं वास्तुमध्ये तु यो बलिः॥ १६॥
नूनमस्मिन्भविष्यति। वास्तुपूजामकुर्वाणस्तवाहारो भविष्यति॥ १७॥
Seeing the Devas thus predominant and finding himself thus beseiged
the being, that sprung from Siva's sweat, said- "O Devas' you have now
made me motionless; be pleased; how can I stay, thus imprisoned, with
my head downward?” The Devas replied- “You will enjoy the
sacrificial offerings of the Visvedeva sacrifice and the offerings
that will be given within any dwelling-house and one who will perform
sacrifices without the prescribed method will also be your food.
अज्ञानातु कृतो यज्ञस्तवाहारो भविष्यति। यज्ञोत्सवादौ च बलिस्तवाहारो
एवमुक्तस्ततो हृष्टः स वास्तुरभवत्तदा। वास्तुयज्ञः
स्मृतस्तस्मात्ततः प्रभृति शान्तये॥ १९॥
You will also enjoy the sacrificial offerings made in course of other
ordinary sacrifices. That Vastu Deva then became highly pleased to
hear those words and since then the Vastu worship became extant to
appease Vastu Deva.”
From that time it became requisite to follow some rules laid by Vastu Sastra to please Vastu Deva.