What is the need for a special purpose entity/vehicle in mortgage backed securities What is the need to create a special purpose entity/vehicle (SPV) for mortgage backed securities? What potential risks are avoided due to the creation of SPV? 

It is my naive idea that the holder of the security always has the first priority right for receiving repayment and interest on the loans. How is this affected in the scenario where the bank transfers the assets to an investment bank vs the SPV transfers the assets to an investment bank, which in turn is securitized?
Here are a few reasons that build on @Dismalscience's answer. 


Capital requirements: Banks don't typically need to hold capital
against loans they originated but subsequently moved into an SPV. This might be regulatory arbitrage but it might be a socially efficient outcome meant to move assets and liabilities out of the banking system.
Market segmentation: Because SPVs can tranche (slice) their loans they can offer products with different assumed default correlations, prepayment risk, and aggregate credit risk. These products can appeal to customers with different beliefs and preferences over risk. 
Bank business model: Banks may be better at finding customers, screening them, and handling the paperwork of making loans (broadly originating) then they are at proving inexpensive and patient capital to fund those loans. Securitization allows the bank to specialize in origination without also being the ultimate capital provider.
Information destruction: Because SPVs contain loans that are diversified over geography and borrower characteristics they can reduce market adverse selection, improving pricing and liquidity.