Let’s start by distinguishing the thematic roles in this sentence, the roles played by the actual entities named with respect to the actual action described by the verb, and the syntactic roles, the formal roles played by the words with respect to the structure of the sentence.
In your examples, a pen has a thematic role linguists call Theme, the thing which is ‘acted upon’, and the syntactic role Direct Object. I’ll notate that like this, with the abbreviation for the thematic role in superscript after the word and the abbreviation for the syntactic role before the word:
You, the person who receives the thing given, is assigned the thematic role Recipient. That thematic role is the same throughout; but that role is expressed syntactically in two different ways:
The Recipient may be expressed as a noun phrase: a bare pronoun like you or a fuller phrase like my friend Mary. In this case we call its syntactic role an Object—specifically, an Indirect Object.
IOyouR … IOmy friend MaryR
The Recipient may also be expressed with a preposition phrase (PP) headed by to: to you, to my friend Mary. In this case, we do not call the PP an Object but a Complement.
Cto youR … Cto my friend MaryR
Whether to is used—whether the Recipient is expressed as an IO or as a C—determines how your sentence is laid out.
A Recipient expressed as an IO comes before the DO, the Theme.
I gave [IOyouR] [DOa penT].
John is giving [IOMaryR] [DOa ringT].
The order of the two Objects tells us which is the IO and which the DO, and that in turn lets us know which is the Recipient and which the Theme.
But suppose you want to put your Recipient after your Theme? In this case you have to express R as a C, as a PP with to, since the position no longer makes it clear what role the noun phrase plays in the sentence:
I gave [DOa penT] [Cto youR] .
John is giving [DOa ringT] [Cto MaryR].
The same holds with other verbs which take Indirect Objects, such as say or serve:
The waiter has served [IOthe partyR] [DOdinnerT]. ... The waiter has served [DOdinnerT] [Cto the partyR].
I will tell [IOthe policeR] [DOmy storyT]. ... I will tell [DOmy storyT] [C to the policeR].
In all these sentences, the Recipient is the person or party who receives the action. An Recipient may also be the person or party who is the beneficiary of the action; in that case the proper preposition is for, not to, but otherwise the same rules apply:
George is building [IOhis daughterR] [DOa houseT]. ... George is building [DOa houseT] [Cfor his daughterR].