How does contextual menus work in Drupal 7?
I'll put my temporary solution here for now anyway in case it will help someone.

What I did was just creating a new theme function (?) in my module

$themes['contextual'] = array(
    'template' =&gt; 'contextual',
    'variables' =&gt; array('links' =&gt; array(), 'destination' =&gt; null)
);


Then I defined my contextual.tpl.php

&lt;div class="contextual-links-wrapper"&gt;
    &lt;ul class="contextual-links"&gt;
        &lt;?php foreach($links as $url =&gt; $title): ?&gt;
        &lt;li&gt;
            &lt;?php echo l($title, $url, 
                array('query' =&gt; array('destination' =&gt; $destination))) ?&gt;
        &lt;/li&gt;
        &lt;?php endforeach; ?&gt;
    &lt;/ul&gt;
&lt;/div&gt;


Finally, to use it, wherever there is a need for a contextual menu, add the class contextual-links-region to the container div of the node (or whatever), and inside the div I use my function, for example: 

&lt;?php echo theme('contextual', array('destination' =&gt; 'node/'.arg(1),
    'links' =&gt; array('node/'.$somenode-&gt;nid.'/edit', t('Edit node')))); ?&gt;